CSC297.Java, Class 29: Efficient Sorting Algorithms Admin: * Other free times? We now meet 8:30-10:00 MW, 9:00-10:00 F * New homework: * Implement Radix Sort on strings Topics: * Old Homework * Finishing quickSort * Finishing mergeSort * Other sorts (radix) Write prepost for sortInPlace /** * Procedure: * sortInPlace * Parameters: * stuff, an array of objects * compare, a comparator * Purpose: * Rearrange stuff so that it is in "sorted order". * Produces: * Nothing; called for its side effects. * Preconditions: * stuff is nonempty. * compare.mayPrecede can be applied to any pair of objects in stuff. * compare.mayPrecede is transitive. * Postconditions: * stuff may have changed. * stuff is sorted, that is for all 0 <= x < y < stuff.length * compare.mayPrecede(stuff[x], stuff[y]) * alternately, for all 0 <= i < stuff.length-1 * compare.mayPrecede(stuff[i], stuff[i+1]) * The resulting contents of stuff are a permutation of the original * contents of stuff. */ * Try to come up with a quick way of sorting a group of numbers between 1 and 100 when you may have duplicates. Hint: Think about the post office. * What the post-office does: Put 'em in piles by the first digit. * Then sort each of those piles by the second digit. * Etc. * That is called "radix sort". * Bucket sort * Build an array indexed by 0 ... 100 * Look at each number and "put it" in the right place in the array * Read the values back out of the array * Each bucket can be a list or even simply a counter (depending on what is associated with the number) What is the running time of bucket sort? Use N for the number of items to sort and M for the maximum value: * N steps to put them in the buckets (assuming it takes one step to put in the correct bucket) * M steps to look at each bucket * N steps to shove those back into the sorted array * O(2N + M) = O(N + M) * If M is in O(N), this is a very efficient sorting algorithm * Unfortunately, it only works really well for numbers. Post-office Radix Sort: * First organize by leftmost digit * Then rearrange each pile by the next digit * Then by the next * And so on and so forth Running time of Radix Sort? * Each rearrange is N steps. * If there are K digits in the numbers, we rearrange K times * O(K*N) * If K is constant (e.g., if you're sorting Java integers), this is an O(N) algorithms * If K is unbounded (e.g., N can be O(K)), this is unbounded * Only use radix sort if K is "small" Time to write partition * Remember: The goal is to take a subarray and rearrange it so that small things are at the left and large things are at the right. * Strategy: * One index starts at the right * One index starts at the left * Move the right index left as long as it refers to large things * Move the left thing right as long as it refers to small things * Swap the two * Repeat until the two indices cross. [1,2,3,4], pivot 1 l r Move r left over big things [1,2,3,4] l r Detour: * Q: What is that "String[] args" in main? * A: It gives you access to stuff typed on the command line.