David Luebke 2
10/08/25
SortingSo Far
īŦ Insertion sort:
īŽ Easy to code
īŽ Fast on small inputs (less than ~50 elements)
īŽ Fast on nearly-sorted inputs
īŽ O(n2
) worst case
īŽ O(n2
) average (equally-likely inputs) case
īŽ O(n2
) reverse-sorted case
3.
David Luebke 3
10/08/25
SortingSo Far
īŦ Merge sort:
īŽ Divide-and-conquer:
īĩ Split array in half
īĩ Recursively sort subarrays
īĩ Linear-time merge step
īŽ O(n lg n) worst case
īŽ Doesnât sort in place
4.
David Luebke 4
10/08/25
SortingSo Far
īŦ Heap sort:
īŽ Uses the very useful heap data structure
īĩ Complete binary tree
īĩ Heap property: parent key > childrenâs keys
īŽ O(n lg n) worst case
īŽ Sorts in place
īŽ Fair amount of shuffling memory around
5.
David Luebke 5
10/08/25
SortingSo Far
īŦ Quick sort:
īŽ Divide-and-conquer:
īĩ Partition array into two subarrays, recursively sort
īĩ All of first subarray < all of second subarray
īĩ No merge step needed!
īŽ O(n lg n) average case
īŽ Fast in practice
īŽ O(n2
) worst case
īĩ Naïve implementation: worst case on sorted input
īĩ Address this with randomized quicksort
6.
David Luebke 6
10/08/25
HowFast Can We Sort?
īŦ We will provide a lower bound, then beat it
īŽ How do you suppose weâll beat it?
īŦ First, an observation: all of the sorting algorithms so
far are comparison sorts
īŽ The only operation used to gain ordering information about
a sequence is the pairwise comparison of two elements
īŽ Theorem: all comparison sorts are ī(n lg n)
īĩ A comparison sort must do O(n) comparisons (why?)
īĩ What about the gap between O(n) and O(n lg n)
7.
David Luebke 7
10/08/25
DecisionTrees
īŦ Decision trees provide an abstraction of
comparison sorts
īŽ A decision tree represents the comparisons made
by a comparison sort. Every thing else ignored
īŽ (Draw examples on board)
īŦ What do the leaves represent?
īŦ How many leaves must there be?
8.
David Luebke 8
10/08/25
DecisionTrees
īŦ Decision trees can model comparison sorts. For a
given algorithm:
īŽ One tree for each n
īŽ Tree paths are all possible execution traces
īŽ Whatâs the longest path in a decision tree for insertion
sort? For merge sort?
īŦ What is the asymptotic height of any decision tree
for sorting n elements?
īŦ Answer: ī(n lg n) (now letâs prove itâĻ)
9.
David Luebke 9
10/08/25
LowerBound For
Comparison Sorting
īŦ Thm: Any decision tree that sorts n elements
has height ī(n lg n)
īŦ Whatâs the minimum # of leaves?
īŦ Whatâs the maximum # of leaves of a binary
tree of height h?
īŦ Clearly the minimum # of leaves is less than or
equal to the maximum # of leaves
10.
David Luebke 10
10/08/25
LowerBound For
Comparison Sorting
īŦ So we haveâĻ
n! īŖ 2h
īŦ Taking logarithms:
lg (n!) īŖ h
īŦ Stirlingâs approximation tells us:
īŦ Thus:
n
e
n
n īˇ
ī¸
īļ
ī§
ī¨
īĻ
īž
!
n
e
n
h īˇ
ī¸
īļ
ī§
ī¨
īĻ
īŗlg
11.
David Luebke 11
10/08/25
LowerBound For
Comparison Sorting
īŦ So we have
īŦ Thus the minimum height of a decision tree is ī(n lg n)
ī¨ īŠ
n
n
e
n
n
n
e
n
h
n
lg
lg
lg
lg
ī
īŊ
ī
īŊ
īˇ
ī¸
īļ
ī§
ī¨
īĻ
īŗ
12.
David Luebke 12
10/08/25
LowerBound For
Comparison Sorts
īŦ Thus the time to comparison sort n elements is
ī(n lg n)
īŦ Corollary: Heapsort and Mergesort are
asymptotically optimal comparison sorts
īŦ But the name of this lecture is âSorting in
linear timeâ!
īŽ How can we do better than ī(n lg n)?
13.
David Luebke 13
10/08/25
SortingIn Linear Time
īŦ Counting sort
īŽ No comparisons between elements!
īŽ ButâĻdepends on assumption about the numbers
being sorted
īĩ We assume numbers are in the range 1.. k
īŽ The algorithm:
īĩ Input: A[1..n], where A[j] ī {1, 2, 3, âĻ, k}
īĩ Output: B[1..n], sorted (notice: not sorting in place)
īĩ Also: Array C[1..k] for auxiliary storage
14.
David Luebke 14
10/08/25
CountingSort
1 CountingSort(A, B, k)
2 for i=1 to k
3 C[i]= 0;
4 for j=1 to n
5 C[A[j]] += 1;
6 for i=2 to k
7 C[i] = C[i] + C[i-1];
8 for j=n downto 1
9 B[C[A[j]]] = A[j];
10 C[A[j]] -= 1;
Work through example: A={4 1 3 4 3}, k = 4
15.
David Luebke 15
10/08/25
CountingSort
1 CountingSort(A, B, k)
2 for i=1 to k
3 C[i]= 0;
4 for j=1 to n
5 C[A[j]] += 1;
6 for i=2 to k
7 C[i] = C[i] + C[i-1];
8 for j=n downto 1
9 B[C[A[j]]] = A[j];
10 C[A[j]] -= 1;
What will be the running time?
Takes time O(k)
Takes time O(n)
16.
David Luebke 16
10/08/25
CountingSort
īŦ Total time: O(n + k)
īŽ Usually, k = O(n)
īŽ Thus counting sort runs in O(n) time
īŦ But sorting is ī(n lg n)!
īŽ No contradiction--this is not a comparison sort (in
fact, there are no comparisons at all!)
īŽ Notice that this algorithm is stable
17.
David Luebke 17
10/08/25
CountingSort
īŦ Cool! Why donât we always use counting
sort?
īŦ Because it depends on range k of elements
īŦ Could we use counting sort to sort 32 bit
integers? Why or why not?
īŦ Answer: no, k too large (232
= 4,294,967,296)
18.
David Luebke 18
10/08/25
CountingSort
īŦ How did IBM get rich originally?
īŦ Answer: punched card readers for census
tabulation in early 1900âs.
īŽ In particular, a card sorter that could sort cards into
different bins
īĩ Each column can be punched in 12 places
īĩ Decimal digits use 10 places
īŽ Problem: only one column can be sorted on at a
time
19.
David Luebke 19
10/08/25
RadixSort
īŦ Intuitively, you might sort on the most significant
digit, then the second msd, etc.
īŦ Problem: lots of intermediate piles of cards (read:
scratch arrays) to keep track of
īŦ Key idea: sort the least significant digit first
RadixSort(A, d)
for i=1 to d
StableSort(A) on digit i
īŽ Example: Fig 9.3
20.
David Luebke 20
10/08/25
RadixSort
īŦ Can we prove it will work?
īŦ Sketch of an inductive argument (induction on the
number of passes):
īŽ Assume lower-order digits {j: j<i}are sorted
īŽ Show that sorting next digit i leaves array correctly sorted
īĩ If two digits at position i are different, ordering numbers by that
digit is correct (lower-order digits irrelevant)
īĩ If they are the same, numbers are already sorted on the lower-
order digits. Since we use a stable sort, the numbers stay in the
right order
21.
David Luebke 21
10/08/25
RadixSort
īŦ What sort will we use to sort on digits?
īŦ Counting sort is obvious choice:
īŽ Sort n numbers on digits that range from 1..k
īŽ Time: O(n + k)
īŦ Each pass over n numbers with d digits takes
time O(n+k), so total time O(dn+dk)
īŽ When d is constant and k=O(n), takes O(n) time
īŦ How many bits in a computer word?
22.
David Luebke 22
10/08/25
RadixSort
īŦ Problem: sort 1 million 64-bit numbers
īŽ Treat as four-digit radix 216
numbers
īŽ Can sort in just four passes with radix sort!
īŦ Compares well with typical O(n lg n) comparison
sort
īŽ Requires approx lg n = 20 operations per number being
sorted
īŦ So why would we ever use anything but radix sort?
23.
David Luebke 23
10/08/25
RadixSort
īŦ In general, radix sort based on counting sort is
īŽ Fast
īŽ Asymptotically fast (i.e., O(n))
īŽ Simple to code
īŽ A good choice
īŦ To think about: Can radix sort be used on
floating-point numbers?