11. Sorting Algorithms

This chapter discusses algorithms for sorting a set of $ \mathtt{n}$ items. This might seem like a strange topic for a book on data structures, but there are several good reasons for including it here. The most obvious reason is that two of these sorting algorithms (quicksort and heap-sort) are intimately related to two of the data structures we have already studied (random binary search trees and heaps, respectively).

The first part of this chapter discusses algorithms that sort using only comparisons and presents three algorithms that run in $ O(\ensuremath{\mathtt{n}}\log \ensuremath{\mathtt{n}})$ time. As it turns out, all three algorithms are asymptotically optimal; no algorithm that uses only comparisons can avoid doing roughly $ \ensuremath{\mathtt{n}}\log \ensuremath{\mathtt{n}}$ comparisons in the worst-case and even the average-case.

The second part of this chapter shows that, if we allow other operations besides comparisons, then all bets are off. Indeed, by using array-indexing, it is possible to sort a set of $ \mathtt{n}$ integers in the range $ \{0,\ldots,\ensuremath{\mathtt{n}}^c-1\}$ in $ O(c\ensuremath{\mathtt{n}})$ time.