



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Given an input of n numbers to sort, they could be arranged in n! different orders. Clearly, for each of those n! cases, a sorting algorithm MUST "act" differently. In particular, if a sorting algorithm makes a certain number of comparisons, when running to each of these n! inputs, no two will give the same exact results for every comparison. (If they did, could you sort those two different data sets differently?)
Thus, we must make enough comparisons, in any comparison based sorting algorithm to distinguish between all n! inputs. A single comparison can distinguish between 2 separate inputs. (Because either an element is greater than the other, or less than the other. For the moment we are assuming distinct elements.) In general, k comparisons can distinguish between at most 2 k^ inputs. To see this, imagine making a chart like so:
Input a[1]>a[2] a[1]>a[3] a[2]>a[3], ... a[n-2]>a[n-1] 2,1,4,3 T F F T 2,3,1,4 F T T F 4,3,2,1 T T T T
As you can see, if there are k columns in the chart, each with 2 possible answers, there are a total of 2 k^ possible distinct rows that could be on the chart. If two distinct inputs gave rise to the same EXACT row of answers, it would be impossible for our sorting algorithm to distinguish between the two inputs. Thus, if a sorting algorithm is to work, each input NEEDS to lead to a distinct row of answers.
Consider sorting the following list: 1.3, 0.7, 0.1, 1.8, 1.1, 1.0, 0.5, 1.7, 0.3, and 1.5. Here is a picture of what happens during the sort. Based upon this, imagine how one would write code to implement this sort. (The data structure to use would be an array of linked lists.)
Also, consider that it is not necessary for the input range to start at 0. As long as you have both a lower and upper bound for input, the bucket sort algorithm can be adapted.
Given that the range of inputs is [L, L+N), (where L stands for lowest possible value, and N stands for the range), and there are n values to be sorted, if we are given the value v, our first goal is to determine WHICH linked list to insert the value into. Once we do that, then we can simply call our linked list insert method to insert a value into a linked list in sorted order.
The correct array index is floor(n(v - L)/N), using integer division. To see this, consider that the value v-L is from the range [0, N). Now, we must map this to the correct array index from 0 to n-1. Each array index has a range of N/n values. Thus, what we want to do is divide our value v-L by the range of each bucket to give us the array index of the appropriate bucket. For example, using our previous list, if we were to sort 1.3, it would go to the bucket floor(10(1.3 - 0)/2) = 6 as desired.
Although I will skip the mathematics as to why this sort is O(n), I will give you the intuition as to why that is the case. Since we have as many buckets and values to sort, IF the values to sort are randomly distributed, then the length of each linked list will be constant with very high probability. Each insert in a constant sized linked list will take constant time, just the total amount of time for the sort will be linear in the number of items to sort.
The input to this sort must be non-negative integers all of a fixed length of digits. Let each number be k digits long. The sort works as follows:
Once again, this sort is much more easily understood with a demonstration. The running time of this sort should be O(nk), since we do k stable sorts that each run in O(n) time.
Depending on how many digits the numbers are, this sort can be more efficient than any O(nlgn) sort, depending on the range of values.
A stable sort is one where if two values being sorted, say vi and v (^) j are equal, and v (^) i comes before v (^) j in the sorted list, then vi will STILL come before vj in the sorted list.
This sort does seem quite counter intuitive. One question to ask: would it work the other way around (namely from most significant to least significant digit)? If not, could we adapt the sort to work the other way around? Why does this always work? Why does the intermediate sort need to be stable?
The key to it is as follows: After the mth iteration, the values are sorted in order with respect to the m least significant digits. Clearly, when m=k, this means the list is just plain sorted!