Lecture 3
Lecture 3
help50
style50
check50
printf
debug50
ddb
help50
style50
check50
printf
debug50
ddb
searching
input → → output
→ → output
→ → bool
algorithms
running times
O
time to solve
size of problem
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)
Ω
Ω(n2)
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1)
linear search
For i from 0 to n-1
If number behind i'th door
Return true
Return false
O(n2)
O(n log n)
O(n)
O(log n)
O(1)
O(n2)
O(n log n)
O(log n)
O(1)
Ω(n2)
Ω(n log n)
Ω(n)
Ω(log n)
Ω(1)
Ω(n2)
Ω(n log n)
Ω(n)
Ω(log n)
O(n log n)
O(log n)
O(1)
O(n2)
O(n log n)
O(1)
Ω(n2)
Ω(n log n)
Ω(n)
Ω(log n)
Ω(n log n)
Ω(n)
Ω(log n)
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)
O(n log n)
O(1)
O(n2) selection sort
O(n log n)
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)
Ω(n log n)
Ω(n)
Ω(log n)
Ω(n log n)
Ω(n)
Ω(log n)
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) selection sort
O(n log n)
O(1)
O(n2) selection sort, bubble sort
O(n log n)
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
If no swaps
Quit
Ω(n2) selection sort
Ω(n log n)
Ω(n)
Ω(log n)
Ω(n log n)
Ω(log n)
O(n log n)
O(1)
O(n2) selection sort, bubble sort
O(1)
Ω(n2) selection sort
Ω(n log n)
Ω(log n)
Ω(log n)
Θ(n log n)
Θ(n)
Θ(log n)
Θ(1)
Θ(n2) selection sort
Θ(n)
Θ(log n)
Θ(1)
This is CS50