Array sorting is a fundamental operation in many applications. The requirement is to sort n elements of an array
into increasing order. For this to make sense, the elements must be comparable with respect to order. So we assume that array elements implement the Comparable interface. Note that the array itself may be of length greater than n. The specification only requires that the first n elements should be sorted.a[0], ...,a[n-1]
There are a number of efficient algorithms which run in
time on average. Binary heaps provide such an
algorithm, and one which has a number of attractive features. A
simple implementation would be to insert the array elements, one by
one, into a priority queue, implemented by a binary heap, and then to
remove them one by one. It is true they would then come out in
reverse order, but it would be simple to revise the binary heap code
to implement an ``inverse priority'' queue in which the smallest
element emerges first.
There are, however, two related ways in which we can improve on this simple algorithm: