• Java 2 Platform Standard Edition 5.0 API specification
• OpenJDK source code repository for library classes
• Source code from Data structures and problem-solving using Java, third edition
Today we'll examine another sorting method with a running time that is O(n lg n). This method uses a priority queue as an intermediate data structure.
The idea is simple: Given an unsorted array, create an empty priority queue that can hold elements of that array. Traverse the array, adding each element in turn to the priority queue. Then remove the elements one at a time from the priority queue, storing them back into the array, from left to right. Since at each step the element that can be extracted from the priority queue is the least of the remaining items, it goes into the leftmost position of the array that has not already been filled.
Since this algorithm involves n invocations of the priority
queue's add method and n invocations of its remove
method to sort an array of size n, and since each of this
invocations (according to the documentation for java.util.PriorityQueue) requires no more than O(lg n) time, the entire algorithm is O(n lg n).
The file /home/stone/courses/java/examples/SortTester.java
defines a class SortTester that runs a sorting method on a randomly
constructed array of Doubles and reports how long it takes to
execute that method. It also contains a stub for the priorityQueueSort sorting method.
Implement the priorityQueueSort method. The priority queue
that it uses as an intermediate data structure should be stored in a local
variable of the method.
Revise the SortTester program so that it calls priorityQueueSort repeatedly, with array sizes of 10000, 20000, 30000, and
so on up to 160000, and reports the running times. What pattern do you
expect to see if the priorityQueueSort method is really O(n lg n)? What pattern would you expect to see if
the method were O(n2)?
Revise the main method so that it invokes the sorting method
provided by java.util.Arrays instead of priorityQueueSort and
re-run it. Do the running times exhibit the O(n lg
n) pattern? Are they less than the corresponding times for
priorityQueueSort? Is this consistent with both sorting algorithms
having running times of the same order?
Revise your SparseFinder program from the lab on
demographics so that it stores the County
objects in an array rather than a list. Revise the County class so
that it implements the Comparable interface. Then, returning to
SparseFinder, use the priority queue sort to arrange the elements of
the array of County in order of increasing population density
immediately after you read it in. Revise findSparsest so that it
simply returns the County that is in position 0 of the sorted array.
Modify SparseFinder so that it prints out the names and population
densities of the ten most sparsely populated counties in the state of Iowa,
in order of increasing population density.