Ninja is given an array of integers ‘ARR’ of size ‘N’. He has to sort the given array in increasing order using radix sort.
Ninja called you for help as you are his only friend. Help him to solve the problem.
Example :Input: ‘N’ = ‘4’ , 'V' = [ 2 , 1 , 4 , 3 ]
Output: "1 2 3 4"
As [ 1 , 2 , 3 , 4 ] is the sorted order.
The first line will contain the integer 'T', denoting the number of test cases.
For each test case, the first line contains a single integer ‘N’ the size of the input array.
Second-line contains ‘N’ space-separated integers which are the elements of the input array.
Output format :
For each test case, print the element of the array input array in increasing order.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= 'T' <= 10
1 <= ‘N’ <= 10^4
1 <= ARR[i] <= 10^9
Time Limit: 1 sec
2
4
1 4 3 5
5
5 4 3 2 1
1 3 4 5
1 2 3 4 5
In test case ‘1’, ‘1 3 4 5’ is the increasing sorted order.
In test case ‘2’. ‘1 2 3 4 5’ is the increasing sorted order.
2
2
30 25
6
2 2 2 2 2 2
25 30
2 2 2 2 2 2
What happens in the counting sort?
In, Radix Sort we do digit by digit sorting. We start from the least significant digit to the most significant digit. Radix sort uses counting sort as a subroutine to sort.
Let's see how the counting sort works.
This we will do for all positions in a number(from least significant to most significant).
The steps are as follows :
6. Return ‘arr’.
O( N * log10( max(ARR) ) ), Where 'N' is the number of elements in the array and max(ARR) is the maximum element in array 'ARR'.
We are doing the counting sort which takes ~O(N) for the number of digits that is (log10( max(v) ) times.
Hence, the time complexity is O( N * log10( max(ARR) ) ).
O(N), Where ‘N’ is the size of the vector
We are using another vector to store ~N element in sorted order and then copying it to the given vector.
Hence the space complexity is O(N).