You’re given an array with many repeated values. Your task is to sort this array efficiently.
The first line of the input contains an integer T denoting the number of test cases.
The first line of each test case contains an integer N, the size of the array.
The second line of each test case contains N space-separated integers, the elements present in the array.
Output Format :
For every test case, the only line of output should contain N space-separated integers in sorted order.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
10^-9<= arr[i] <= 10^9
Time limit: 1 sec
1
4
3 1 2 4
1 2 3 4
‘1 2 3 4’, is the sorted array for the given input.
2
6
3 4 1 6 2 5
5
3 5 8 9 11
1 2 3 4 5 6
3 5 8 9 11
‘1 2 3 4 5 6’ and ‘3 5 8 9 11’ are the sorted arrays for the given inputs.
Divide and Conquer
In the QuickSort Algorithm, we do the partitioning of the array into smaller arrays. We partition our array into two smaller arrays such that elements of one sub-array are smaller than the pivot element and the elements of the other sub-array are greater than the pivot element.
Algorithm:
O(N^2), where ‘N’ is the number of elements in the given array.
When the pivot element is chosen such that it lies to either side of the array, it will make one empty array and another array with N-1 elements. Therefore the quick sort algorithm will be called on this array with N - 1 elements only. The recursive call for these N -1 elements will take N - 1 time, then for N - 2 elements, it will take N - 2 time, and it continues like that. Hence, the worst time complexity becomes O(N^2).
O(N), where ‘N’ is the number of elements in the given array.
In the worst case, we have N recursive calls in the stack space. Thus, the space complexity will be O(N).