CS50 for JDs
Algorithms, Data Structures
abstraction
seminars
Innovative Algorithms and Their Applications
Fri, Jan 6, 9:00 AM – 11:00 AM EST in WCC Room 1010, �with Catherine Deskur
shorts
input →
→ output
algorithms
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
linear search
For each door from left to right
If number is behind door
Answer is true
Answer is false
For each door from left to right
If number is behind door
Answer is true
Else
Answer is false
binary search
If number behind middle door
Return true
Else if number < middle door
Search left half
Else if number > middle door
Search right half
If no doors
If number behind middle door
Return true
Else if number < middle door
Search left half
Else if number > middle door
Search right half
If no doors
Return false
If number behind middle door
Return true
Else if number < middle door
Search left half
Else if number > middle door
Search right half
size of problem
time to solve
size of problem
time to solve
O(n)
O(n/2)
O(log2n)
size of problem
time to solve
O(n)
O(n/2)
O(log2n)
size of problem
time to solve
O(n)
O(n)
O(log n)
O(n2)
O(n log n)
O(n)
O(log n)
O(1)
O(n2)
O(n log n)
O(n) linear search
O(log n) binary search
O(1)
Ω(n2)
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1)
Ω(n2)
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1) linear search, binary search
input →
→ output
unsorted →
→ output
unsorted →
→ sorted
6 3 8 5 2 7 4 1
selection sort
For i from 0 to n-1
Find smallest item between i'th item and last item
Swap smallest item with i'th item
(n – 1)
(n – 1) + (n – 2)
(n – 1) + (n – 2) + (n – 3)
(n – 1) + (n – 2) + (n – 3) + ... + 1
(n – 1) + (n – 2) + (n – 3) + ... + 1
n(n – 1)/2
(n – 1) + (n – 2) + (n – 3) + ... + 1
n(n – 1)/2
(n2 – n)/2
(n – 1) + (n – 2) + (n – 3) + ... + 1
n(n – 1)/2
(n2 – n)/2
n2/2 – n/2
(n – 1) + (n – 2) + (n – 3) + ... + 1
n(n – 1)/2
(n2 – n)/2
n2/2 – n/2
O(n2)
O(n2) selection sort
O(n log n)
O(n)
O(log n)
O(1)
Ω(n2) selection sort
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1)
6 3 8 5 2 7 4 1
bubble sort
Repeat n-1 times
For i from 0 to n-2
If i'th and i+1'th elements out of order
Swap them
(n – 1) × (n – 1)
(n – 1) × (n – 1)
n2 – 1n – 1n + 1
(n – 1) × (n – 1)
n2 – 1n – 1n + 1
n2 – 2n + 1
(n – 1) × (n – 1)
n2 – 1n – 1n + 1
n2 – 2n + 1
O(n2)
O(n2) bubble sort
O(n log n)
O(n)
O(log n)
O(1)
Ω(n2) bubble sort
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1)
bubble sort
Repeat n-1 times� For i from 0 to n-2� If i'th and i+1'th elements out of order� Swap them
Repeat until no swaps� For i from 0 to n-2� If i'th and i+1'th elements out of order� Swap them
O(n2) bubble sort
O(n log n)
O(n)
O(log n)
O(1)
Ω(n2)
Ω(n log n)
Ω(n) bubble sort
Ω(log n)
Ω(1)
recursion
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If person is on page
5 Call person
6 Else if person is earlier in book
7 Open to middle of left half of book
8 Go back to line 3
9 Else if person is later in book
10 Open to middle of right half of book
11 Go back to line 3
12 Else
13 Quit
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If person is on page
5 Call person
6 Else if person is earlier in book
7 Open to middle of left half of book
8 Go back to line 3
9 Else if person is later in book
10 Open to middle of right half of book
11 Go back to line 3
12 Else
13 Quit
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If person is on page
5 Call person
6 Else if person is earlier in book
7 Open to middle of left half of book
8 Go back to line 3
9 Else if person is later in book
10 Open to middle of right half of book
11 Go back to line 3
12 Else
13 Quit
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If person is on page
5 Call person
6 Else if person is earlier in book
7 Search left half of book
8
9 Else if person is later in book
10 Search right half of book
11
12 Else
13 Quit
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If person is on page
5 Call person
6 Else if person is earlier in book
7 Search left half of book
8 Else if person is later in book
9 Search right half of book
10 Else
11 Quit
merge sort
If only one item
Return
Else
Sort left half of items
Sort right half of items
Merge sorted halves
If only one item
Return
Else
Sort left half of items
Sort right half of items
Merge sorted halves
7 | 4 | 5 | 2 | 6 | 3 | 8 | 1 |
7 | 4 | 5 | 2 | 6 | 3 | 8 | 1 |
7 | 4 | 5 | 2 | 6 | 3 | 8 | 1 |
7 | 4 | 5 | 2 | 6 | 3 | 8 | 1 |
7 | 4 | 5 | 2 | 6 | 3 | 8 | 1 |
7 | 4 | 5 | 2 | 6 | 3 | 8 | 1 |
7 | | 5 | 2 | 6 | 3 | 8 | 1 |
4 | | | | | | | |
| | 5 | 2 | 6 | 3 | 8 | 1 |
4 | 7 | | | | | | |
| | 5 | 2 | 6 | 3 | 8 | 1 |
4 | 7 | | | | | | |
| | 5 | 2 | 6 | 3 | 8 | 1 |
4 | 7 | | | | | | |
| | 5 | 2 | 6 | 3 | 8 | 1 |
4 | 7 | | | | | | |
| | 5 | 2 | 6 | 3 | 8 | 1 |
4 | 7 | | | | | | |
| | 5 | | 6 | 3 | 8 | 1 |
4 | 7 | 2 | | | | | |
| | | | 6 | 3 | 8 | 1 |
4 | 7 | 2 | 5 | | | | |
| | | | 6 | 3 | 8 | 1 |
4 | 7 | 2 | 5 | | | | |
| | | | 6 | 3 | 8 | 1 |
4 | 7 | | 5 | | | | |
2 | | | | | | | |
| | | | 6 | 3 | 8 | 1 |
| 7 | | 5 | | | | |
2 | 4 | | | | | | |
| | | | 6 | 3 | 8 | 1 |
| 7 | | | | | | |
2 | 4 | 5 | | | | | |
| | | | 6 | 3 | 8 | 1 |
| | | | | | | |
2 | 4 | 5 | 7 | | | | |
| | | | 6 | 3 | 8 | 1 |
| | | | | | | |
2 | 4 | 5 | 7 | | | | |
| | | | 6 | 3 | 8 | 1 |
| | | | | | | |
2 | 4 | 5 | 7 | | | | |
| | | | 6 | 3 | 8 | 1 |
| | | | | | | |
2 | 4 | 5 | 7 | | | | |
| | | | 6 | 3 | 8 | 1 |
| | | | | | | |
2 | 4 | 5 | 7 | | | | |
| | | | 6 | 3 | 8 | 1 |
| | | | | | | |
2 | 4 | 5 | 7 | | | | |
| | | | 6 | | 8 | 1 |
| | | | 3 | | | |
2 | 4 | 5 | 7 | | | | |
| | | | | | 8 | 1 |
| | | | 3 | 6 | | |
2 | 4 | 5 | 7 | | | | |
| | | | | | 8 | 1 |
| | | | 3 | 6 | | |
2 | 4 | 5 | 7 | | | | |
| | | | | | 8 | 1 |
| | | | 3 | 6 | | |
2 | 4 | 5 | 7 | | | | |
| | | | | | 8 | 1 |
| | | | 3 | 6 | | |
2 | 4 | 5 | 7 | | | | |
| | | | | | 8 | 1 |
| | | | 3 | 6 | | |
2 | 4 | 5 | 7 | | | | |
| | | | | | 8 | 1 |
| | | | 3 | 6 | 1 | |
2 | 4 | 5 | 7 | | | | |
| | | | | | | 1 |
| | | | 3 | 6 | 1 | 8 |
2 | 4 | 5 | 7 | | | | |
| | | | | | | 1 |
| | | | 3 | 6 | 1 | 8 |
2 | 4 | 5 | 7 | | | | |
| | | | | | | 1 |
| | | | 3 | 6 | | 8 |
2 | 4 | 5 | 7 | 1 | | | |
| | | | | | | 1 |
| | | | | 6 | | 8 |
2 | 4 | 5 | 7 | 1 | 3 | | |
| | | | | | | 1 |
| | | | | | | 8 |
2 | 4 | 5 | 7 | 1 | 3 | 6 | |
| | | | | | | 1 |
| | | | | | | 8 |
2 | 4 | 5 | 7 | 1 | 3 | 6 | 8 |
| | | | | | | 1 |
| | | | | | | 8 |
2 | 4 | 5 | 7 | 1 | 3 | 6 | 8 |
| | | | | | | 1 |
| | | | | | | 8 |
2 | 4 | 5 | 7 | | 3 | 6 | 8 |
1 | | | | | | | |
| | | | | | | 1 |
| | | | | | | 8 |
| 4 | 5 | 7 | | 3 | 6 | 8 |
1 | 2 | | | | | | |
| | | | | | | 1 |
| | | | | | | 8 |
| 4 | 5 | 7 | | | 6 | 8 |
1 | 2 | 3 | | | | | |
| | | | | | | 1 |
| | | | | | | 8 |
| | 5 | 7 | | | 6 | 8 |
1 | 2 | 3 | 4 | | | | |
| | | | | | | 1 |
| | | | | | | 8 |
| | | 7 | | | 6 | 8 |
1 | 2 | 3 | 4 | 5 | | | |
| | | | | | | 1 |
| | | | | | | 8 |
| | | 7 | | | | 8 |
1 | 2 | 3 | 4 | 5 | 6 | | |
| | | | | | | 1 |
| | | | | | | 8 |
| | | | | | | 8 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | |
| | | | | | | 1 |
| | | | | | | 8 |
| | | | | | | |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| | | | | | | 1 |
7 | 4 | 5 | 2 | 6 | 3 | 8 | 1 |
| | | | | | | 1 |
| | | | | | | 8 |
7 | 4 | 5 | 2 | 6 | 3 | 8 | 1 |
4 | 7 | 2 | 5 | 3 | 6 | 1 | 8 |
| | | | | | | 1 |
| | | | | | | 8 |
| | | | | | | |
7 | 4 | 5 | 2 | 6 | 3 | 8 | 1 |
4 | 7 | 2 | 5 | 3 | 6 | 1 | 8 |
2 | 4 | 5 | 7 | 1 | 3 | 6 | 8 |
| | | | | | | 1 |
| | | | | | | 8 |
| | | | | | | |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
7 | 4 | 5 | 2 | 6 | 3 | 8 | 1 |
4 | 7 | 2 | 5 | 3 | 6 | 1 | 8 |
2 | 4 | 5 | 7 | 1 | 3 | 6 | 8 |
O(n2)
O(n log n) merge sort
O(n)
O(log n)
O(1)
Ω(n2)
Ω(n log n) merge sort
Ω(n)
Ω(log n)
Ω(1)
data structures
bool Boolean value
float floating-point value
int integer
str string
...
dict
list
range
set
tuple
...
arrays
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| 1 | 2 | 3 | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| 1 | 2 | 3 | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| 1 | 2 | 3 | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
? | ? | ? | ? | ? | ? | ? | ? |
? | 1 | 2 | 3 | ? | ? | ? | ? |
? | ? | ? | ? | ? | ? | ? | ? |
? | ? | ? | | | | | |
| | | | | | | |
| | | | | | | |
| 1 | 2 | 3 | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| 1 | 2 | 3 | | | | |
| | | | | | | |
| | | 1 | | | | |
| | | | | | | |
| | | | | | | |
| 1 | 2 | 3 | | | | |
| | | | | | | |
| | | 1 | 2 | | | |
| | | | | | | |
| | | | | | | |
| 1 | 2 | 3 | | | | |
| | | | | | | |
| | | 1 | 2 | 3 | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | 1 | 2 | 3 | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | 1 | 2 | 3 | 4 | |
| | | | | | | |
O(n2)
O(n log n)
O(n)
O(log n)
O(1)
O(n2)
O(n log n)
O(n) insert
O(log n) search
O(1)
linked lists
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| 1 0x123 | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| 1 0x123 | | | | | | |
| | | 2 0x456 | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| 1 0x123 | | | | | | |
| | | 2 0x456 | | | | |
| | | | | 3 0x789 | | |
| | | | | | | |
| | | | | | | |
| 1 0x123 | | | | | | |
| | | 2 0x456 | | | | |
| | | | | 3 0x789 | | |
| | | | | | | |
| | | | | | | |
| 1 0x123 | | | | | | |
| 0x456 | | 2 0x456 | | | | |
| | | | | 3 0x789 | | |
| | | | | | | |
| | | | | | | |
| 1 0x123 | | | | | | |
| 0x456 | | 2 0x456 | | | | |
| | | 0x789 | | 3 0x789 | | |
| | | | | | | |
| | | | | | | |
| 1 | | | | | | |
| | | 2 | | | | |
| | | | | 3 | | |
| | | | | | | |
| | | | | | | |
dict
list
range
set
tuple
...
trees
binary search trees
| | | | | | | | |
| | | | | | | | |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
| | | | | | | | |
| | | | | | | | |
4 |
2 |
6 |
1 |
3 |
5 |
7 |
4 |
2 |
6 |
1 |
3 |
5 |
7 |
hash tables
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Albus
Albus
Zacharias
Albus
Hermione
Zacharias
Albus
Ginny
Hermione
Ron
Zacharias
Fred
Severus
Petunia
Draco
James
Cedric
Luna
Neville
Kingsley
Minerva
Vernon
Albus
Ginny
Hermione
Ron
Zacharias
Fred
Severus
Petunia
Draco
James
Cedric
Luna
Neville
Kingsley
Minerva
Vernon
Harry
Albus
Ginny
Hermione
Ron
Zacharias
Fred
Severus
Petunia
Draco
James
Cedric
Luna
Neville
Kingsley
Minerva
Vernon
Harry
Hagrid
Albus
Ginny
Hermione
Ron
Zacharias
Fred
Severus
Petunia
Draco
James
Cedric
Luna
Neville
Kingsley
Minerva
Vernon
Harry
Hagrid
Sirius
Remus
George
Lily
Lucius
input →
→ output
hash function
Albus →
→ 0
Zacharias →
→ 25
Haa
Hab
Hac
Had
Hae
Haf
Hag
...
Haq
Har
Has
...
Heq
Her
Hes
Hermione
Harry
Hagrid
O(n2)
O(n log n)
O(n)
O(log n)
O(1)
O(n2)
O(n log n)
O(n) search
O(log n)
O(1)
O(n2)
O(n log n)
O(n) search
O(log n)
O(1) insert
Albus
Ginny
Hermione
Ron
Zacharias
Fred
Severus
Petunia
Draco
James
Cedric
Luna
Neville
Kingsley
Minerva
Vernon
Harry
Hagrid
Sirius
Remus
George
Lily
Lucius
Lavender
dictionaries
dict
list
range
set
tuple
...
queues
stacks
Lab 0
Assignment 2
Office Hours
CS50 for JDs
Algorithms, Data Structures