

Ninja’s teacher has given him a task to sort a list of the numbers containing N numbers. But the teacher has added the condition that as the range of the numbers in the list is limited, he must do this task in linear time complexity.
Your task is to help Ninja to complete the task.
ExampleIf n = 10 and nums = [1, 6, 0, -1, 9, 4, 32, 76, -98, 3], then the sorted list would be [-98, -1, 0, 1, 3, 4, 6, 9, 32, 76].
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.
The first line of the input contains an integer ‘N’ denoting the number of elements in the list.
The next line contains N space-separated integers denoting the elements in the list.
Output Format
For each test case return the sorted list.
Note:- You don’t need to take input or print anything. This already has been taken care of. Just implement the function.
1 <= T <= 100
1 <= N <= 5000
-10^3 <= nums[i] <= 10^3, where 0<=i<N-1
2
5
1 2 8 3 0
7
1 -1 9 0 3 4 8
0 1 2 3 8
-1 0 1 3 4 8 9
For the first test case,
Given array is [1,2,8,3,0]
So the sorted array will be [0,1,2,3,8]
For the second test case,
Given array is [1,-1,9,0,3,4,8]
So the sorted array will be [-1,0,1,3,4,8,9]
2
6
4 3 2 1 0 -1
5
1 2 3 4 5
-1 0 1 2 3 4
1 2 3 4 5
Think of implementing a sorting algorithm, similar to hashing.
Approach:
In count sort first, we find the number of the object with the specific key values, similar to what we do in hashing.
Then we will find the position of each object in the output.
Algorithm:
numCount[a[i] - min]+=1; where ‘a’ is the given array and min is the minimum element in the given array.
numCount[i] = numCount[i] + numCount[i-1].
ans[numCount[a[i] - min] - 1] = a[i];
O(n+k)
where ‘n’ is the length of a given array and ‘k’ is the range of elements in the given array.
As we will iterate every element once and we will also iterate the numCount array which is of the size of the range of the given array.
O(n+k)
where ‘n’ is the length of a given array and ‘k’ is the range of elements in the given array.
The numCount array will store k elements in the array and ans array will store n elements.