1
WARNING, THESE SLIDES HAVE NOT YET BEEN UPDATED FOR FALL 2026
2
What questions do you have about Fall 2026 classes?
yellkey.com/end
Insertion Sort and Quicksort
3
Lecture 33 (Sorting 2)
CS61B, Spring 2026 @ UC Berkeley
Josh Hug and Manuel Sabin
Naive Insertion Sort
Lecture 33, CS61B, Spring 2026
Insertion Sort
Quicksort
4
Insertion Sort
General strategy:
Naive approach, build entirely new output:
5
32
15
2
17
19
26
41
17
17
Input:
Output:
Insertion Sort
General strategy:
Naive approach, build entirely new output:
6
32
15
2
17
19
26
41
17
17
Input:
Output:
Insertion Sort
General strategy:
Naive approach, build entirely new output:
7
32
15
2
17
19
26
41
17
17
32
Input:
Output:
Insertion Sort
General strategy:
Naive approach, build entirely new output:
8
32
15
2
17
19
26
41
17
17
32
15
Input:
Output:
Insertion Sort
General strategy:
Naive approach, build entirely new output:
9
32
15
2
17
19
26
41
17
17
32
15
2
Input:
Output:
Insertion Sort
General strategy:
Naive approach, build entirely new output:
10
32
15
2
17
19
26
41
17
17
32
15
2
17
Input:
Output:
Insertion Sort
General strategy:
Naive approach, build entirely new output:
11
32
15
2
17
19
26
41
17
17
32
15
2
17
19
Input:
Output:
Insertion Sort
General strategy:
Naive approach, build entirely new output:
12
32
15
2
17
19
26
41
17
17
32
15
2
17
19
26
Input:
Output:
Insertion Sort
General strategy:
Naive approach, build entirely new output:
13
32
15
2
17
19
26
41
17
17
32
15
2
17
19
26
41
Input:
Output:
Insertion Sort
General strategy:
Naive approach, build entirely new output:
14
32
15
2
17
19
26
41
17
17
32
15
2
17
19
26
41
17
Input:
Output:
Insertion Sort
General strategy:
Naive approach, build entirely new output:
15
32
15
2
17
19
26
41
17
17
32
15
2
17
19
26
41
17
17
Input:
Output:
Can we eliminate this output array?
In-Place Insertion Sort
Lecture 33, CS61B, Spring 2026
Insertion Sort
Quicksort
16
Insertion Sort
General strategy:
For naive approach, if output sequence contains k items, worst cost to insert a single item is k.
More efficient method:
17
Insertion Sort
General strategy:
18
32
15
2
17
19
26
41
17
17
Input:
Insertion Sort
General strategy:
19
32
15
2
17
19
26
41
17
17
i
Input:
In-place Insertion Sort
General strategy:
20
32
15
2
17
19
26
41
17
17
i
sorted
unexamined
Input:
In-place Insertion Sort
General strategy:
21
32
15
2
17
19
26
41
17
17
i
sorted
unexamined
Input:
In-place Insertion Sort
General strategy:
22
32
15
2
17
19
26
41
17
17
i
sorted
unexamined
Input:
In-place Insertion Sort
General strategy:
23
15
32
2
17
19
26
41
17
17
i
unexamined
Input:
In-place Insertion Sort
General strategy:
24
15
32
2
17
19
26
41
17
17
i
sorted
unexamined
Input:
In-place Insertion Sort
General strategy:
25
15
32
2
17
19
26
41
17
17
i
j
sorted
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
26
15
2
32
17
19
26
41
17
17
i
j
Note: We’ve temporarily broken our invariant that the items up through item i should be sorted!
In example above: Use j pointer to track current spot of traveling item.
unexamined
Input:
In-place Insertion Sort
General strategy:
27
2
15
32
17
19
26
41
17
17
i
j
Once the traveller settles, the invariant is restored.
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
28
2
15
32
17
19
26
41
17
17
i
j
sorted
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort: yellkey.com/service
General strategy:
29
2
15
32
17
19
26
41
17
17
i
j
sorted
unexamined
Input:
current spot of traveling item
while (/* FILL ME IN */ ) {
// Swap backwards
}
In-place Insertion Sort
General strategy:
30
2
15
32
17
19
26
41
17
17
i
j
sorted
unexamined
Input:
current spot of traveling item
while (j > 0 && input[j - 1] > input[j]) {
// Swap backwards
}
In-place Insertion Sort
General strategy:
31
2
15
17
32
19
26
41
17
17
i
j
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
32
2
15
17
32
19
26
41
17
17
i
j
sorted
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
33
2
15
17
32
19
26
41
17
17
j
sorted
i
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
34
2
15
17
19
32
26
41
17
17
i
j
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
35
2
15
17
19
32
26
41
17
17
i
j
sorted
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
36
2
15
17
19
32
26
41
17
17
i
j
sorted
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
37
2
15
17
19
26
32
41
17
17
i
j
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
38
2
15
17
19
26
32
41
17
17
i
j
sorted
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
39
2
15
17
19
26
32
41
17
17
i
j
sorted
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
40
2
15
17
19
26
32
41
17
17
i
j
sorted
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort: yellkey.com/impact
General strategy:
41
2
15
17
19
26
32
41
17
17
i
j
sorted
unexamined
Input:
How many swaps do we need to do to get 17 in the correct place?
current spot of traveling item
In-place Insertion Sort
General strategy:
42
2
15
17
19
26
32
17
41
17
i
j
unexamined
Input:
Use j pointer to track current spot of traveling item.
1 swap so far
In-place Insertion Sort
General strategy:
43
2
15
17
19
26
17
32
41
17
i
j
unexamined
Input:
Use j pointer to track current spot of traveling item.
2 swaps so far
In-place Insertion Sort
General strategy:
44
2
15
17
19
17
26
32
41
17
i
j
unexamined
Input:
Use j pointer to track current spot of traveling item.
3 swaps so far
In-place Insertion Sort
General strategy:
45
2
15
17
17
19
26
32
41
17
i
j
unexamined
Input:
Use j pointer to track current spot of traveling item.
4 swaps so far
In-place Insertion Sort
General strategy:
46
2
15
17
17
19
26
32
41
17
i
j
unexamined
Input:
Use j pointer to track current spot of traveling item.
4 swaps total
In-place Insertion Sort
General strategy:
47
2
15
17
17
19
26
32
41
17
i
j
sorted
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
48
2
15
17
17
19
26
32
41
17
i
j
sorted
unexamined
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
49
2
15
17
17
19
26
32
17
41
i
j
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
50
2
15
17
17
19
26
17
32
41
i
j
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
51
2
15
17
17
19
17
26
32
41
i
j
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
52
2
15
17
17
17
19
26
32
41
i
j
Input:
Use j pointer to track current spot of traveling item.
In-place Insertion Sort
General strategy:
53
2
15
17
17
17
19
26
32
41
Input:
i
j
sorted
Use j pointer to track current spot of traveling item.
In-place Insertion Sort: Reflections on Naming
54
2
15
17
17
17
19
26
32
41
Input:
i
j
sorted
From John Ousterhout’s Software Design Lecture
Use j pointer to track current spot of traveling item.
In-place Insertion Sort: Reflections on Naming: yellkey.com/love
What new names would you propose for i and j?
Should we take a more extreme step?
55
2
15
17
17
17
19
26
32
41
Input:
i
j
sorted
current spot of traveling item
In-place Insertion Sort: Reflections on Naming
What new names would you propose for i and j? Should we take a more extreme step?
Your ideas:
56
2
15
17
17
17
19
26
32
41
Input:
i
j
sorted
current spot of traveling item
In-place Insertion Sort: Reflections on Naming
What new names would you propose for i and j? Should we take a more extreme step?
John Ousterhout’s suggestions:
Kay’s ideas:
57
2
15
17
17
17
19
26
32
41
Input:
i
j
sorted
current spot of traveling item
Insertion Sort Runtime
Lecture 33, CS61B, Spring 2026
Insertion Sort
Miscellaneous Sorts
Quicksort
58
In-place Insertion Sort
Two more examples.
7 swaps:
36 swaps:
59
P O T A T O
P O T A T O (0 swaps)
O P T A T O (1 swap )
O P T A T O (0 swaps)
A O P T T O (3 swaps)
A O P T T O (0 swaps)
A O O P T T (3 swaps)
S O R T E X A M P L E
S O R T E X A M P L E (0 swaps)
O S R T E X A M P L E (1 swap )
O R S T E X A M P L E (1 swap )
O R S T E X A M P L E (0 swaps)
E O R S T X A M P L E (4 swaps)
E O R S T X A M P L E (0 swaps)
A E O R S T X M P L E (6 swaps)
A E M O R S T X P L E (5 swaps)
A E M O P R S T X L E (4 swaps)
A E L M O P R S T X E (7 swaps)
A E E L M O P R S T X (8 swaps)
Purple: Element that we’re moving left (with swaps).
Black: Elements that got swapped with purple.
Grey: Not considered this iteration.
Insertion Sort Runtime: yellkey.com/she
What is the best- and worst-case runtime of insertion sort?
36 swaps:
60
Insertion Sort Runtime
What is the best- and worst-case runtime of insertion sort?
36 swaps:
61
Picking the Best Sort: yellkey.com/notice
Suppose we do the following:
Which sorting algorithm would be the fastest choice for X?
62
Recall: Inversions
An inversion is a pair of elements that are out of order with respect to <.
Another way to state the goal of sorting:
�What’s the worst case number of inversions?
63
2
15
17
19
26
17
32
41
17
Recall: Inversions
An inversion is a pair of elements that are out of order with respect to <.
Another way to state the goal of sorting:
64
19-17
(out of 36 inversions)
2
15
17
19
26
17
32
41
17
Recall: Inversions
An inversion is a pair of elements that are out of order with respect to <.
Another way to state the goal of sorting:
65
19-17, 19-17
(out of 36 inversions)
2
15
17
19
26
17
32
41
17
Recall: Inversions
An inversion is a pair of elements that are out of order with respect to <.
Another way to state the goal of sorting:
66
19-17, 19-17, 26-17
(out of 36 inversions)
2
15
17
19
26
17
32
41
17
Recall: Inversions
An inversion is a pair of elements that are out of order with respect to <.
Another way to state the goal of sorting:
67
19-17, 19-17, 26-17, 26-17
(out of 36 inversions)
2
15
17
19
26
17
32
41
17
Recall: Inversions
An inversion is a pair of elements that are out of order with respect to <.
Another way to state the goal of sorting:
68
19-17, 19-17, 26-17, 26-17
32-17
(out of 36 inversions)
2
15
17
19
26
17
32
41
17
Recall: Inversions
An inversion is a pair of elements that are out of order with respect to <.
Another way to state the goal of sorting:
69
19-17, 19-17, 26-17, 26-17
32-17, 41-17 = 6 total inversions
(out of 36 inversions)
2
15
17
19
26
17
32
41
17
Observation: Each swap in Insertion Sort reduces the number of inversions by 1
Inversions between red items don't change (because they don't move)
Inversions between a red and blue item don't change (their relative position in the array stays the same)
We fix one inversion within the blue items
So total number of inversions decrease by 1 every swap
70
2
15
17
19
26
17
32
41
17
i
j
2
15
17
19
17
26
32
41
17
i
j
Observation: Insertion Sort on Almost Sorted Arrays
For arrays that are almost sorted, insertion sort does very little work.
71
Picking the Best Sort: How many inversions?
Suppose we do the following:
What’s the worst-case number of inversions here?
Example shorter array: [9, 12, 25, 34, 55]
What one number would you change to introduce the most inversions?
72
Picking the Best Sort: How many inversions?
Suppose we do the following:
What’s the worst-case number of inversions here? N - 1
Example shorter array: [9, 12, 25, 34, 55]
What one number would you change to introduce the most inversions?
73
Picking the Best Sort
Suppose we do the following:
In the worst case, we have N - 1 inversions (Θ(N) inversions)
Which sorting algorithm would be the fastest choice for X? Worst case run-times:
74
Picking the Best Sort
Suppose we do the following:
In the worst case, we have N - 1 inversions (Θ(N) inversions)
Which sorting algorithm would be the fastest choice for X? Run-times:
75
Number of swaps = number of inversions!
Insertion Sort Sweet Spots
On arrays with a small number of inversions, insertion sort is extremely fast.
Less obvious: For small arrays (N < 15 or so), insertion sort is fastest.
76
Sorts So Far
77
See this link for bonus slides on Shell's Sort, an optimization of insertion sort.
| Best Case Runtime | Worst Case Runtime | Space | Notes |
Θ(N2) Find smallest item in remaining items, N times | Θ(N2) | Θ(1) | | |
(in place) | Θ(N)�All duplicates | Θ(N log N) | Θ(1)** | Bad cache (61C) performance. |
Θ(N log N) | Θ(N log N) | Θ(N) | Faster than heapsort. | |
Insertion Sort�(in place) | Θ(N)�cN Inversions | Θ(N2) | Θ(1) | Best for small N or almost sorted. |
Quicksort Backstory, Partitioning
Lecture 33, CS61B, Spring 2026
Insertion Sort
Quicksort
78
Sorting So Far
Core ideas:
Quicksort:
79
Context for Quicksort’s Invention (Source)
1960: Tony Hoare was working on a crude automated translation program for Russian and English. �
80
... | ... |
beautiful | красивая |
... | ... |
cat | кошка |
... | ... |
“The cat wore a beautiful hat.”
Dictionary of D english words
N words
“Кошка носил красивая шапка.”
How would you do this?
Context for Quicksort’s Invention (Source)
Limitation at the time:
1960: Tony Hoare was working on a crude automated translation program for Russian and English.
Algorithm: N binary searches of D length dictionary.
81
... | ... |
beautiful | красивая |
... | ... |
cat | кошка |
... | ... |
Partitioning
Main idea: I have a list of small and large items. To sort this:
How to decide what's big and what's small?
We'll call this a partition; we separate the big items and the small items, and put remaining items in the middle.
82
Partitioning
A partition of an array, given a pivot x, is a rearrangement of the items so that:
83
3
1
2
4
6
8
7
3
4
1
2
6
7
8
Example of a valid output
Another example of a valid output
6
8
3
1
2
7
4
Input
9
9
9
Core Idea of Tony’s Sort: Partitioning http://yellkey.com/TODO
A partition of an array, given a pivot x, is a rearrangement of the items so that:
Which partitions are valid?
84
5
550
10
4
10
9
330
4
5
9
10
10
330
550
5
9
10
4
10
550
330
5
4
9
10
10
550
330
5
9
10
4
10
550
330
A.
C.
B.
D.
j
j
j
j
The Core Idea of Tony’s Sort: Partitioning
A partition of an array, given a pivot x, is a rearrangement of the items so that:
Which partitions are valid?
85
5
550
10
4
10
9
330
i
4
5
9
10
10
330
550
5
9
10
4
10
550
330
5
4
9
10
10
550
330
5
9
10
4
10
550
330
A.
C.
B.
D.
j
j
j
j
Called the ‘pivot’.
Job Interview Style Question (Partitioning)
Given an array of colors where the 0th element is white (and maybe a few more), and the remaining elements are red (less) or blue (greater), rearrange the array so that all red squares are to the left of the white square, the white squares end up together, and all blue squares are to the right. Your algorithm must complete in Θ(N) time (no space restriction).
86
3
1
2
4
6
8
7
3
4
1
2
6
7
8
Example of a valid output
Another example of a valid output
6
8
3
1
2
7
4
Input
6
6
6
Quicksort
Lecture 33, CS61B, Spring 2026
Insertion Sort
Miscellaneous Sorts
Quicksort
87
Quick Sort: Intuition
88
1
2
3
4
5
6
7
8
0
32
15
2
17
19
26
41
17
17
Input:
unsorted
32
Pick one item (“pivot”), get it in the correct spot
Quick Sort: Intuition
89
1
2
3
4
5
6
7
8
0
32
15
2
17
19
26
41
17
17
Input:
unsorted
32
Pick one item (“pivot”), get it in the correct spot
32
Quick Sort: Intuition
90
1
2
3
4
5
6
7
8
0
32
15
2
17
19
26
41
17
17
Input:
unsorted
32
Pick one item (“pivot”), get it in the correct spot
32
Get everything else on the correct side
32
Quick Sort: Intuition
91
1
2
3
4
5
6
7
8
0
32
15
2
17
19
26
41
17
17
Input:
unsorted
32
Pick one item (“pivot”), get it in the correct spot
32
Get everything else on the correct side
2
17
19
26
17
17
32
15
Quick Sort: Intuition
92
1
2
3
4
5
6
7
8
0
32
15
2
17
19
26
41
17
17
Input:
unsorted
32
Pick one item (“pivot”), get it in the correct spot
32
Get everything else on the correct side
2
17
19
26
17
17
32
41
15
<= 32
>= 32
Quick Sort: Intuition
93
1
2
3
4
5
6
7
8
0
32
15
2
17
19
26
41
17
17
Input:
unsorted
32
Pick one item (“pivot”), get it in the correct spot
32
Get everything else on the correct side
2
17
19
26
17
17
32
41
15
Do this to each item => sorted array
<= 32
>= 32
Each time have to look through fewer items => quick
Core Idea of Tony’s Sort: Partitioning http://yellkey.com/TODO
A partition of an array, given a pivot x, is a rearrangement of the items so that:
Which partitions are valid?
94
5
550
10
4
10
9
330
4
5
9
10
10
330
550
5
9
10
4
10
550
330
5
4
9
10
10
550
330
5
9
10
4
10
550
330
A.
C.
B.
D.
j
j
j
j
The Core Idea of Tony’s Sort: Partitioning
A partition of an array, given a pivot x, is a rearrangement of the items so that:
Which partitions are valid?
95
5
550
10
4
10
9
330
i
4
5
9
10
10
330
550
5
9
10
4
10
550
330
5
4
9
10
10
550
330
5
9
10
4
10
550
330
A.
C.
B.
D.
j
j
j
j
Called the ‘pivot’.
Partition Sort, a.k.a. Quicksort
Observations:
96
5
3
2
1
8
4
6
7
3
2
1
4
7
8
6
5
3
2
1
4
5
7
8
6
5
2
1
3
4
5
6
7
8
5
Q: How would we use this operation for sorting?
Quick Sort
Quick sorting N items:
97
32
15
2
17
19
26
41
17
17
Input:
unsorted
Quick Sort
Quick sorting N items:
98
32
15
2
17
19
26
41
17
17
Input:
partition(32)
Quick Sort
Quick sorting N items:
99
15
2
17
19
26
17
17
32
41
Input:
<= 32
>= 32
in its place
partition(32)
Quick Sort
Quick sorting N items:
100
15
2
17
19
26
17
17
32
Input:
in its place
41
partition(32)
Quick Sort
Quick sorting N items:
101
partition(32)
partition(15)
partition(2)
partition(17)
partition(19)
partition(17)
partition(17)
partition(26)
x
x
x
x
x
x
x
2
15
17
17
17
19
26
32
41
Input:
in its place
in its place
in its place
in its place
in its place
in its place
in its place
in its place
x
Quick Sort
Quick sorting N items:
102
partition(32)
partition(15)
partition(2)
partition(17)
partition(19)
partition(17)
partition(17)
partition(26)
x
x
x
x
x
x
x
2
15
17
17
17
19
26
32
41
Input:
in its place
in its place
in its place
in its place
in its place
in its place
in its place
in its place
in its place
If you don't fully trust the recursion, see these extra slides for a complete demo.
x
Partition Sort, a.k.a. Quicksort
Quick sorting N items:
103
32
15
2
17
19
26
41
17
17
partition(32)
15
2
17
19
26
17
17
32
41
<= 32
>= 32
in its place
Quicksort
Quicksort was the name chosen by Tony Hoare for partition sort.
How fast is Quicksort? Need to count number and difficulty of partition operations.
Theoretical analysis:
104