Chapter 8: Sorting in Linear Time

| < Day Day Up > |

# Chapter 8: Sorting in Linear Time

## Overview

We have now introduced several algorithms that can sort n numbers in O(n lg n) time. Merge sort and heapsort achieve this upper bound in the worst case; quicksort achieves it on average. Moreover, for each of these algorithms, we can produce a sequence of n input numbers that causes the algorithm to run in Θ(n lg n) time.

These algorithms share an interesting property: the sorted order they determine is based only on comparisons between the input elements. We call such sorting algorithms comparison sorts. All the sorting algorithms introduced thus far are comparison sorts.

In Section 8.1, we shall prove that any comparison sort must make Θ(n lg n) comparisons in the worst case to sort n elements. Thus, merge sort and heapsort are asymptotically optimal, and no comparison sort exists that is faster by more than a constant factor.

Sections 8.2, 8.3, and 8.4 examine three sorting algorithms-counting sort, radix sort, and bucket sort-that run in linear time. Needless to say, these algorithms use operations other than comparisons to determine the sorted order. Consequently, the Θ(n lg n) lower bound does not apply to them.

| < Day Day Up > |