Implement Counting Sort. It works by counting the occurrences of each distinct element, then using arithmetic to determine the positions of each element in the output sequence.
Count frequency of each element (given a known range 0..k). Compute prefix sums to get positions. Place each element in its correct output position.
- Find min and max of the array to determine range k.
- Create count[] of size k+1 and count each element.
- Compute prefix sums: count[i] += count[i-1].
- Iterate original array backwards and place each element at count[arr[i]]-1.
- Time Complexity: O(N + K) where K is range
- Space Complexity: O(N + K)