This is CS50
input → → output
→ → output
→ → bool
algorithms
linear search
For i from 0 to n-1
If i'th element is 50
Return true
Return false
binary search
If middle item is 50
Return true
Else if 50 < middle item
Search left half
Else if 50 > middle item
Search right half
If no items
If middle item is 50
Return true
Else if 50 < middle item
Search left half
Else if 50 > middle item
Search right half
If no items
Return false
If middle item is 50
Return true
Else if 50 < middle item
Search left half
Else if 50 > middle item
Search right half
time to solve
size of problem
O(n) O(n/2)
time to solve
O(log2n)
size of problem
O
O(n) O(n/2)
time to solve
O(log2n)
size of problem
O(n) O(n/2)
time to solve
O(log2n)
size of problem
O(n) O(n)
time to solve
O(log2n)
size of problem
O(n) O(n)
time to solve
O(log n)
size of problem
O(n)
time to solve
O(log n)
size of problem
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)
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
Ω(n2)
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1) linear search, binary search
int numbers[]
string names[]
person people[]
string name;
string number;
typedef struct
{
string name;
string number;
}
person;
input → → output
unsorted → → output
unsorted → → sorted
7 2 1 6 3 4 50 → → sorted
7 2 1 6 3 4 50 → → 1 2 3 4 6 7 50
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
O
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)
O(n log n)
O(n) linear search
O(log n) binary search
O(1)
O(n2) bubble sort
O(n log n)
O(n) linear search
O(log n) binary search
O(1)
Ω
Repeat n-1 times
For i from 0 to n-2
If i'th and i+1'th elements out of order
Swap them
Ω(n2)
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1) linear search, binary search
Ω(n2) bubble sort
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1) linear search, binary search
selection sort
6 3 8 5 2 7 4 1
For i from 0 to n-1
Find smallest item between i'th item and last item
Swap smallest item with i'th item
O
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
n + (n – 1)
n + (n – 1) + (n – 2)
n + (n – 1) + (n – 2) + ... + 1
n + (n – 1) + (n – 2) + ... + 1
n(n + 1)/2
n + (n – 1) + (n – 2) + ... + 1
n(n + 1)/2
(n2 + n)/2
n + (n – 1) + (n – 2) + ... + 1
n(n + 1)/2
(n2 + n)/2
n2/2 + n/2
n + (n – 1) + (n – 2) + ... + 1
n(n + 1)/2
(n2 + n)/2
n2/2 + n/2
O(n2)
O(n2) bubble sort
O(n log n)
O(n) linear search
O(log n) binary search
O(1)
O(n2) bubble sort, selection sort
O(n log n)
O(n) linear search
O(log n) binary search
O(1)
Ω
For i from 0 to n-1
Find smallest item between i'th item and last item
Swap smallest item with i'th item
Ω(n2) bubble sort
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1) linear search, binary search
Ω(n2) bubble sort, selection sort
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1) linear search, binary search
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
Ω(n2) bubble sort, selection sort
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1) linear search, binary search
Ω(n2) selection sort
Ω(n log n)
Ω(n) bubble sort
Ω(log n)
Ω(1) linear search, binary search
elections
recursion
1 Pick up phone book
2 Open to middle of phone book
3 Look at page
4 If Smith is on page
5 Call Mike
6 Else if Smith is earlier in book
7 Open to middle of left half of book
8 Go back to line 3
9 Else if Smith 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 Smith is on page
5 Call Mike
6 Else if Smith is earlier in book
7 Open to middle of left half of book
8 Go back to line 3
9 Else if Smith 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 Smith is on page
5 Call Mike
6 Else if Smith is earlier in book
7 Open to middle of left half of book
8
9 Else if Smith is later in book
10 Open to middle of 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 Smith is on page
5 Call Mike
6 Else if Smith is earlier in book
7 Search left half of book
8
9 Else if Smith 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 Smith is on page
5 Call Mike
6 Else if Smith is earlier in book
7 Search left half of book
8
9 Else if Smith 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 Smith is on page
5 Call Mike
6 Else if Smith is earlier in book
7 Search left half of book
8 Else if Smith 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
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
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
2 4 5 7 1 3 6
1
2 4 5 7 1 3 6 8
1
2 4 5 7 1 3 6 8
1
2 4 5 7 3 6 8
1
1
4 5 7 3 6 8
1 2
1
4 5 7 6 8
1 2 3
1
5 7 6 8
1 2 3 4
1
7 6 8
1 2 3 4 5
1
7 8
1 2 3 4 5 6
1
1 2 3 4 5 6 7
1
1 2 3 4 5 6 7 8
7 4 5 2 6 3 8 11
7 4 5 2 6 3 8 11
4 7 2 5 3 6 1 88
7 4 5 2 6 3 8 11
4 7 2 5 3 6 1 88
2 4 5 7 1 3 6 8
7 4 5 2 6 3 8 11
4 7 2 5 3 6 1 88
2 4 5 7 1 3 6 8
1 2 3 4 5 6 7 8
O(n2) bubble sort, selection sort
O(n log n)
O(n) linear search
O(log n) binary search
O(1)
O(n2) bubble sort, selection sort
O(n log n) merge sort
O(n) linear search
O(log n) binary search
O(1)
Ω(n2) selection sort
Ω(n log n)
Ω(n) bubble sort
Ω(log n)
Ω(1) linear search, binary search
Ω(n2) selection sort
Ω(n log n) merge sort
Ω(n) bubble sort
Ω(log n)
Ω(1) linear search, binary search
Θ
Θ(n2)
Θ(n log n)
Θ(n)
Θ(log n)
Θ(1)
Θ(n2) selection sort
Θ(n log n) merge sort
Θ(n)
Θ(log n)
Θ(1)
This is CS50