CSInterviewing
Problems
Register
Login
Problems
Register
Login
menu
Sort Three Integers
algorithm
sorting
# Sort Three Integers ## Difficulty Easy ## Tags `algorithm`, `sorting` ## Problem Statement Given three integers `a`, `b`, and `c`, return them in sorted (ascending) order without using any built-in sort function. ## Constraints - -10⁹ ≤ a, b, c ≤ 10⁹ - Do not use built-in sort functions. ## Signatures ### Python ```python def sort_three(a: int, b: int, c: int) -> List[int]: pass ``` ### Java ```java public class Solution { public List<Integer> sort_three(int a, int b, int c) { return null; } } ``` ### Csharp ```csharp public class Solution { public IList<int> sort_three(int a, int b, int c) { throw new NotImplementedException(); } } ``` ### Cpp ```cpp class Solution { public: vector<int> sort_three(int a, int b, int c) { } }; ``` ### C ```c int* sort_three(int a, int b, int c) { } ``` ### Ruby ```ruby def sort_three(a, b, c) end ``` ### Perl ```perl sub sort_three { my ($self, @args) = @_; } ``` ### Scala ```scala object Solution { def sort_three(a: Int, b: Int, c: Int): List[Int] = { } } ``` ### Haskell ```haskell sort_three :: ... -> ... sort_three args = undefined ``` ### Clojure ```clojure (defn sort_three [a b c] ) ``` ### Scheme ```scheme (define (sort_three a b c) ) ``` ## Examples ### Example 1 **Input:** ``` a = 3, b = 1, c = 2 ``` **Output:** ``` [1, 2, 3] ``` ### Example 2 **Input:** ``` a = 10, b = 10, c = 5 ``` **Output:** ``` [5, 10, 10] ``` ### Example 3 **Input:** ``` a = -5, b = 0, c = 5 ``` **Output:** ``` [-5, 0, 5] ``` ## Hints 1. **Comparison approach**: Use a few if-statements to determine the order. 2. **Bubble sort style**: Swap adjacent if out of order, repeat until sorted. ### Simple Algorithm ```python def sort_three(a, b, c): if a > b: a, b = b, a if b > c: b, c = c, b if a > b: a, b = b, a return [a, b, c] ``` This is essentially one pass of bubble sort, which suffices for 3 elements.
Login required
Please log in to view the solution editor and submit code.
Login
Register