🖨️ Printing Instructions: Press Ctrl/Cmd + P and select "Save as PDF".
1

Quicksort, Randomization, and Lower Bounds

2

Learning Goals

3

Quicksort Introduction

4

The Quicksort Philosophy

5

Algorithm Overview

6

Quicksort(A, p, r)

if p < r
  q = Partition(A, p, r)
  Quicksort(A, p, q)
  Quicksort(A, q+1, r)
7

The Partition Subroutine

8

Goal of Partition

9

Lomuto Partition

10

Lomuto Partition Logic

x = A[r]  // Pivot
i = p - 1
for j = p to r - 1
  if (A[j] <= x) {
    i = i + 1
    swap A[i] with A[j]
  }
swap A[i+1] with A[r]
return i + 1
11

Hoare Partition

12

Hoare Partition Logic

x = A[p]  // Pivot
i = p - 1
j = r + 1
while (i < j){
  while (A[i] < x) i += 1;
  while (A[j] > x) j -= 1;
  if (i < j) {
    swap A[i] with A[j];
    i += 1;
    j -= 1;
  }
}
return j;
13

Example Trace

14

Performance Analysis

15

Worst Case

16

Best Case

17

Balanced Partitions

18

Randomized Quicksort

19

Why Randomize?

20

Expected Runtime

21

Quicksort vs Merge Sort

22

Order Statistics (QuickSelect)

23

The Selection Problem

24

QuickSelect Algorithm

25

QuickSelect Analysis

26

Lower Bounds for Sorting

27

The Limit

28

Decision Tree Model

29

Tree Properties

30

The Lower Bound Proof

31

Linear Time Sorting

32

Beating the Limit

33

Radix Sort

34

Interactive Practice

35

Activity 1: Master Case 1

36

Solution 1

37

Activity 2: Master Case 2

38

Solution 2

39

Activity 3: Master Case 3

40

Solution 3

41

Activity 4: Substitution

42

Solution 4

43

Activity 5: Uneven Split

44

Solution 5

45

Activity 6: Non-Master

46

Solution 6

47

Interactive: Quicksort Partition

48

Supplementary Resources