Sorting Algorithms Review
💬 Discuss: describe how you would sort [7, 2, 9, 4, 1, 6, 3, 8, 5] using:
13A: Sorting and Graphs
August 12, 2026 // Jennifer Zhao
Overview
What will we learn today?
Selection Sort Review…
Selection Sort Demonstration
Merge Sort Review…
Live Demonstration…
In-Place Sorting
In-Place Sorting
What does this function return?
def selection_sort(arr):
for i in range(len(arr)):
smallest = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[smallest]:
smallest = j
arr[i] = temp
arr[i] = arr[smallest]
arr[smallest] = temp
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
merged_list = merge(left, right)
return merged_list
In-Place Sorting
What does this function return?�→ None
What does this function return?�→ None
def selection_sort(arr):
for i in range(len(arr)):
smallest = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[smallest]:
smallest = j
arr[i] = temp
arr[i] = arr[smallest]
arr[smallest] = temp
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
merged_list = merge(left, right)
return merged_list
In-Place Sorting
What does this function return?�→ None
What does this function return?�
def selection_sort(arr):
for i in range(len(arr)):
smallest = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[smallest]:
smallest = j
arr[i] = temp
arr[i] = arr[smallest]
arr[smallest] = temp
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
merged_list = merge(left, right)
return merged_list
In-Place Sorting
What does this function return?�→ None
What does this function return?�→ A sorted list!
def selection_sort(arr):
for i in range(len(arr)):
smallest = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[smallest]:
smallest = j
arr[i] = temp
arr[i] = arr[smallest]
arr[smallest] = temp
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
merged_list = merge(left, right)
return merged_list
In-Place Sorting
This function mutates/changes the original list…
This function creates a new list and returns it!
def selection_sort(arr):
for i in range(len(arr)):
smallest = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[smallest]:
smallest = j
arr[i] = temp
arr[i] = arr[smallest]
arr[smallest] = temp
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
merged_list = merge(left, right)
return merged_list
What Are Graphs?
What Are Graphs?
A graph is:
4
1
7
2
3
5
6
8
0
What Are Graphs?
A graph is:
4
1
7
2
3
5
6
8
0
What Are Graphs?
4
1
7
2
3
5
6
8
0
What Are Graphs?
4
1
7
2
3
5
6
8
0
What Are Graphs?
4
1
7
2
3
5
6
8
0
What Are Graphs?
💬 Discuss: what could you represent with an undirected graph? With a directed graph?
4
1
7
2
3
5
6
8
0
What Are Graphs?
What Are Graphs?
4
1
7
2
3
5
6
8
0
What Are Graphs?
4
1
7
2
3
5
6
8
0
Graph Vocabulary
Graph Vocabulary
Because there is an edge between vertex 1 and �vertex 4, these two vertices are adjacent.
Equivalently, vertex 4 is a neighbor of vertex 1.
4
1
7
2
3
5
6
8
0
Graph Vocabulary
The degree of a node is the number of neighbors.
Vertex 4 has a degree of 3.
4
1
7
2
3
5
6
8
0
Graph Vocabulary
The degree of a node is the number of neighbors.
Vertex 4 has a degree of 3.
Vertex 1 has a degree of 1.
4
1
7
2
3
5
6
8
0
Graph Vocabulary
4
1
7
2
3
5
6
8
0
Graph Vocabulary
💬 Discuss: what is the degree of each vertex in this graph?
4
7
3
5
6
0
2
Graph Vocabulary
💬 Discuss: what is the in-degree and out-degree of each vertex in this graph?
4
7
3
5
6
0
2
Graphs in Code
#1: Adjacency Dictionary
Each element is vertex : [neighbors]
graph = {
____ : ____,
____ : ____,
____ : ____,
____ : ____,
}
2
0
3
1
#1: Adjacency Dictionary
Each element is vertex : [neighbors]
graph = {
0: [2],
1: [2, 3],
2: [0, 1, 3],
3: [1, 2]
}
2
0
3
1
#1: Adjacency Matrix
graph[i][j] is 1 if there is an edge between i and j.
graph = [
[_, _, _, _],
[_, _, _, _],
[_, _, _, _],
[_, _, _, _]
]
2
0
3
1
#1: Adjacency Matrix
graph[i][j] is 1 if there is an edge between i and j.
graph = [
[0, 0, 1, 0],
[0, 0, 1, 1],
[1, 1, 0, 1],
[0, 1, 1, 0]
]
2
0
3
1
Graph Vocabulary
💬 Discuss: write the graph as an adjacency dictionary.
Then, write the graph as an adjacency matrix.
4
7
3
5
6
0
2
Graph Vocabulary
💬 Discuss:
4
3
1
0
2
👩💻 Let's Write Some Code!
What's Next?
What's Next?
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4