Given an input set of n integers in the range [0..n^3-1], provide a linear time sorting algorithm.
This is a review for my test on thursday, and I have no idea how to approach this problem.
since you have a range of integers, then yes, it can be linear, but this won't always be the case. This is also known as "bucket sort".
Bucket sort runs in linear time on the average. Like counting sort, bucket sort is fast because it assumes something about the input.
Sort the given array in linear time. Try It! Solution: If we use Counting Sort, it would take O(n^2) time as the given range is of size n^2. Using any comparison based sorting like Merge Sort, Heap Sort, .. etc would take O(nLogn) time.
Linear sorting: Radix sort. An important property of counting sort is that it is stable, numbers with the same value, appear in the output in the same order as they do in the input. For instance Heap sort is not stable.
Also take a look at related sorts too: pigeonhole sort or counting sort, as well as radix sort as mentioned by Pukku.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With