Given an array of only 0's, 1's, or 2's, write a O(n) function which sorts this array in ascending order.
Implement the function sort
in sort_the_trio.c
. This function will be then be called by the main
function in
test_sort_the_trio.c
.
To run your solution:
$ make
$ ./test_sort_the_trio
The program will read from stdin until ctrl + d
is entered.
It can only read inputs which are either 0, 1, or 2, and they have to be newline or space seperated. These ints will then be passed into an array.
E.g) 0 2 2 1 0 2 1
will create an array with elements 0 2 2 1 0 2 1
respectively.
The program will write to stdout.
It will print each element in the array, allowing you to check if your sort
function correctly sorts the array.
Input:
0 2 1 2 0 1 1 2 1
`ctrl + d`
Output:
0 0 1 1 1 1 2 2 2
Explanation: Input must be in sorted ascending order.
Input:
1 2
0 1 1 2
0
2 1
`ctrl + d`
Output:
0 0 1 1 1 1 2 2 2
Explanation: Input must be in sorted ascending order
test_sort_the_trio.c
.sort
in sort_the_trio.c
, which is given an array of 0's, 1's, or 2's, read from stdin.When you think your program is working, you can use CSE autotest to test your solution.
$ 2521 autotest sort_the_trio
You can view the solution code to this problem here.