Dsap l02.PDF
Dsap l02.PDF
Heikki Peura
[email protected]
Last time
2 / 27
Announcements
3 / 27
The quiz
4 / 27
Who has studied search algorithms before?
5 / 27
Who has studied search algorithms before?
5 / 27
Search algorithms
6 / 27
What is the worst we could do?
7 / 27
Logarithms
Exponentials: 24 = 2 × 2 × 2 × 2 = 16
8 / 27
Logarithms
Exponentials: 24 = 2 × 2 × 2 × 2 = 16
8 / 27
Logarithms
Exponentials: 24 = 2 × 2 × 2 × 2 = 16
8 / 27
Algorithm design: searching a list
9 / 27
What are computers good at?
10 / 27
Goals in designing algorithms
11 / 27
Goals in designing algorithms
11 / 27
Goals in designing algorithms
Efficiency:
I How much time will our computation take?
I How much memory will it need?
11 / 27
Example: linear search
Is x in list A?
Efficiency:
I How much time will our computation take?
I How much memory will it need?
12 / 27
How much time will it take?
13 / 27
How much time will it take?
13 / 27
How much time will it take?
13 / 27
Complexity and input
14 / 27
Complexity and input
14 / 27
Complexity cases
15 / 27
Complexity cases
15 / 27
Example
1 def sum_up_to(n):
2 result = 0 # 1 step
3 while n > 0: # 1 step, n times
4 result = result + n # 2 steps, n times
5 n = n - 1 # 2 steps, n times
6 return result # 1 step
16 / 27
Example
1 def sum_up_to(n):
2 result = 0 # 1 step
3 while n > 0: # 1 step, n times
4 result = result + n # 2 steps, n times
5 n = n - 1 # 2 steps, n times
6 return result # 1 step
Total: 5n + 2 steps
I As n gets large, 2 is irrelevant
I Arguably, so is 5
I It’s the size of the problem that matters
16 / 27
Example
1 def sum_up_to(n):
2 result = 0 # 1 step
3 while n > 0: # 1 step, n times
4 result = result + n # 2 steps, n times
5 n = n - 1 # 2 steps, n times
6 return result # 1 step
Total: 5n + 2 steps
I As n gets large, 2 is irrelevant
I Arguably, so is 5
I It’s the size of the problem that matters
17 / 27
Example
Steps: 202 + 2x + 2x 2
I x small -> first loop dominates (x = 3)
17 / 27
Example
Steps: 202 + 2x + 2x 2
I x small -> first loop dominates (x = 3)
I x large -> last loop dominates (x = 106 )
17 / 27
Example
Steps: 202 + 2x + 2x 2
I x small -> first loop dominates (x = 3)
I x large -> last loop dominates (x = 106 )
I Only need to consider last (nested) loop for large x
17 / 27
Example
Steps: 202 + 2x + 2x 2
I x small -> first loop dominates (x = 3)
I x large -> last loop dominates (x = 106 )
I Only need to consider last (nested) loop for large x
I Does the 2 in 2x 2 matter? For large x, order of growth much
more important
17 / 27
Asymptotic analysis
18 / 27
Asymptotic analysis
18 / 27
Asymptotic analysis
18 / 27
Asymptotic analysis
18 / 27
Asymptotic analysis
18 / 27
Big-O: bound on runtime growth rate
19 / 27
Big-O: bound on runtime growth rate
19 / 27
Big-O: bound on runtime growth rate
19 / 27
Big O tells us how fast the algorithm is
Fast algorithm: worst-case running time grows slowly with input size
20 / 27
Binary search on sorted list
Algorithm for finding x in sorted list L:
I Pick an index i roughly dividing L in half
I If L[i] == x, return True (if nothing left to search return False)
I If not:
I If L[i] > x, repeat search on left half of L
I Otherwise repeat search on right half
21 / 27
Binary search on sorted list
Algorithm for finding x in sorted list L:
I Pick an index i roughly dividing L in half
I If L[i] == x, return True (if nothing left to search return False)
I If not:
I If L[i] > x, repeat search on left half of L
I Otherwise repeat search on right half
Find number 24 in a list L = [9, 24, 32, 56, 57, 61, 59, 99]
21 / 27
Binary search on sorted list
Algorithm for finding x in sorted list L:
I Pick an index i roughly dividing L in half
I If L[i] == x, return True (if nothing left to search return False)
I If not:
I If L[i] > x, repeat search on left half of L
I Otherwise repeat search on right half
Find number 24 in a list L = [9, 24, 32, 56, 57, 61, 59, 99]
First iteration
9 24 32 56 57 59 61 99
9 24 32 56 57 59 61 99
L[i] = 56 > 24 → discard right half and search left half
21 / 27
Binary search on sorted list
Algorithm for finding x in sorted list L:
I Pick an index i roughly dividing L in half
I If L[i] == x, return True (if nothing left to search return False)
I If not:
I If L[i] > x, repeat search on left half of L
I Otherwise repeat search on right half
Find number 24 in a list L = [9, 24, 32, 56, 57, 61, 59, 99]
First iteration
9 24 32 56 57 59 61 99
9 24 32 56 57 59 61 99
L[i] = 56 > 24 → discard right half and search left half
Second iteration
9 24 32 56 57 59 61 99
L[i] = 24 → return True
21 / 27
Binary search complexity
Algorithm for finding x in list L:
I Pick an index i roughly dividing L in half
I If L[i] == x, return True (if nothing left to search return False)
I If not:
I If L[i] > x, repeat search in left half of L
I Otherwise repeat search in right half
22 / 27
Binary search complexity
Algorithm for finding x in list L:
I Pick an index i roughly dividing L in half
I If L[i] == x, return True (if nothing left to search return False)
I If not:
I If L[i] > x, repeat search in left half of L
I Otherwise repeat search in right half
22 / 27
Binary search complexity
Algorithm for finding x in list L:
I Pick an index i roughly dividing L in half
I If L[i] == x, return True (if nothing left to search return False)
I If not:
I If L[i] > x, repeat search in left half of L
I Otherwise repeat search in right half
22 / 27
Binary search complexity
Algorithm for finding x in list L:
I Pick an index i roughly dividing L in half
I If L[i] == x, return True (if nothing left to search return False)
I If not:
I If L[i] > x, repeat search in left half of L
I Otherwise repeat search in right half
22 / 27
Binary search complexity
Algorithm for finding x in list L:
I Pick an index i roughly dividing L in half
I If L[i] == x, return True (if nothing left to search return False)
I If not:
I If L[i] > x, repeat search in left half of L
I Otherwise repeat search in right half
22 / 27
Sorting algorithms
23 / 27
How would you sort a list?
56 24 99 32 9 61 57 79
24 / 27
How would you sort a list?
56 24 99 32 9 61 57 79
9 24 99 32 56 61 57 79
24 / 27
How would you sort a list?
56 24 99 32 9 61 57 79
9 24 99 32 56 61 57 79
9 24 99 32 56 61 57 79
24 / 27
How would you sort a list?
56 24 99 32 9 61 57 79
9 24 99 32 56 61 57 79
9 24 99 32 56 61 57 79
9 24 32 99 56 61 57 79
24 / 27
How would you sort a list?
56 24 99 32 9 61 57 79
9 24 99 32 56 61 57 79
9 24 99 32 56 61 57 79
9 24 32 99 56 61 57 79
9 24 32 56 99 61 57 79
24 / 27
How would you sort a list?
56 24 99 32 9 61 57 79
9 24 99 32 56 61 57 79
9 24 99 32 56 61 57 79
9 24 32 99 56 61 57 79
9 24 32 56 99 61 57 79
9 24 32 56 57 61 99 79
24 / 27
How would you sort a list?
56 24 99 32 9 61 57 79
9 24 99 32 56 61 57 79
9 24 99 32 56 61 57 79
9 24 32 99 56 61 57 79
9 24 32 56 99 61 57 79
9 24 32 56 57 61 99 79
9 24 32 56 57 61 99 79
24 / 27
How would you sort a list?
56 24 99 32 9 61 57 79
9 24 99 32 56 61 57 79
9 24 99 32 56 61 57 79
9 24 32 99 56 61 57 79
9 24 32 56 99 61 57 79
9 24 32 56 57 61 99 79
9 24 32 56 57 61 99 79
9 24 32 56 57 61 79 99
24 / 27
How would you sort a list?
56 24 99 32 9 61 57 79
9 24 99 32 56 61 57 79
9 24 99 32 56 61 57 79
9 24 32 99 56 61 57 79
9 24 32 56 99 61 57 79
9 24 32 56 57 61 99 79
9 24 32 56 57 61 99 79
9 24 32 56 57 61 79 99
In words: Find smallest item and move it to the front (swap with the
first unsorted item). Repeat with remaining unsorted items.
24 / 27
Selection sort algorithm
25 / 27
Selection sort algorithm
Python:
1 def selection_sort(L):
2 M = L[:] # make a copy of list to also preserve original
3 n = len(M)
4 for index in range(n):
5 min_index = find_min_index(M, index) # index with smallest element
6 M[index], M[min_index] = M[min_index], M[index] # swap positions
7 return M
25 / 27
Selection sort complexity
26 / 27
Selection sort complexity
Complexity:
I O(n) passes of main loop
I Each pass: search for the smallest element in O(n)
I Total O(n2 )
26 / 27
Selection sort complexity
Complexity:
I O(n) passes of main loop
I Each pass: search for the smallest element in O(n)
I Total O(n2 )
Can we do better?
26 / 27
Selection sort complexity
Complexity:
I O(n) passes of main loop
I Each pass: search for the smallest element in O(n)
I Total O(n2 )
Can we do better?
I Yes! Merge sort is O(n log n)
I But you can’t do any better than that...
26 / 27
Complexity matters
27 / 27
Complexity matters
27 / 27
Review
28 / 27
Workshop
x = 24 32 56
y = 19 57 61
z=
How to merge two sorted lists into one?
Loop through both lists simultaneously, copy smaller item to new list z
I Compare items at indices i1 = i2 = 0, update with every copy
operation
x = 24 32 56 i1 = 0
y = 19 57 61 i2 = 0
z=
How to merge two sorted lists into one?
Loop through both lists simultaneously, copy smaller item to new list z
I Compare items at indices i1 = i2 = 0, update with every copy
operation
x = 24 32 56 i1 = 0
y = 19 57 61 i2 = 1
z = 19
How to merge two sorted lists into one?
Loop through both lists simultaneously, copy smaller item to new list z
I Compare items at indices i1 = i2 = 0, update with every copy
operation
x = 24 32 56 i1 = 1
y = 19 57 61 i2 = 1
z = 19 24
How to merge two sorted lists into one?
Loop through both lists simultaneously, copy smaller item to new list z
I Compare items at indices i1 = i2 = 0, update with every copy
operation
x = 24 32 56 i1 = 2
y = 19 57 61 i2 = 1
z = 19 24 32
How to merge two sorted lists into one?
Loop through both lists simultaneously, copy smaller item to new list z
I Compare items at indices i1 = i2 = 0, update with every copy
operation
x = 24 32 56 i1 = 3
y = 19 57 61 i2 = 1
z = 19 24 32 56
How to merge two sorted lists into one?
Loop through both lists simultaneously, copy smaller item to new list z
I Compare items at indices i1 = i2 = 0, update with every copy
operation
x = 24 32 56 i1 = 3
y = 19 57 61 i2 = 2
z = 19 24 32 56 57
How to merge two sorted lists into one?
Loop through both lists simultaneously, copy smaller item to new list z
I Compare items at indices i1 = i2 = 0, update with every copy
operation
x = 24 32 56 i1 = 3
y = 19 57 61 i2 = 3
z = 19 24 32 56 57 61
How to merge two sorted lists into one?
Loop through both lists simultaneously, copy smaller item to new list z
I Compare items at indices i1 = i2 = 0, update with every copy
operation
x = 24 32 56 i1 = 3
y = 19 57 61 i2 = 3
z = 19 24 32 56 57 61
28 / 27
Sidebar: recursion
The factorial of n is the product of integers 1, ..., n.
I As a function: fact(n) = n × (n − 1) × (n − 2) × · · · × 2 × 1
I By convention, fact(0) = 1
1 def fact(n):
2 result = 0
3 for i in range(1, n+1):
4 result = result * i
5 return result
6 print(fact(4))
28 / 27
Sidebar: recursion
The factorial of n is the product of integers 1, ..., n.
I As a function: fact(n) = n × (n − 1) × (n − 2) × · · · × 2 × 1
I By convention, fact(0) = 1
1 def fact(n):
2 result = 0
3 for i in range(1, n+1):
4 result = result * i
5 return result
6 print(fact(4))
fact(n) = 1, for n = 0
fact(n) = n × fact(n − 1), for n > 0
28 / 27
Sidebar: recursion
We can also write the factorial as follows:
fact(n) = 1, for n = 0
fact(n) = n × fact(n − 1), for n > 0
28 / 27
Sidebar: recursion
We can also write the factorial as follows:
fact(n) = 1, for n = 0
fact(n) = n × fact(n − 1), for n > 0
1 def fact_rec(n):
2 if n == 0:
3 return 1
4 else:
5 return n*fact_rec(n-1)
6 print(fact_rec(4))
Merge sort:
Merge sort idea
Merge sort:
I Base case: if list length n < 2, the list is sorted
Merge sort idea
Merge sort:
I Base case: if list length n < 2, the list is sorted
I Divide: if list length n ≥ 2, split into two lists and merge sort each
Merge sort idea
Merge sort:
I Base case: if list length n < 2, the list is sorted
I Divide: if list length n ≥ 2, split into two lists and merge sort each
I Combine (merge) the results of the two smaller merge sorts
Merge sort
Dividing
56 24 99 32 9 61 57 79
Merging
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
Merging
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32
Merging
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32
56 24
Merging
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32
56 24
Merging
24 56
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32
56 24 99 32
Merging
24 56
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32
56 24 99 32
Merging
24 56 32 99
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32
56 24 99 32
Merging
24 56 32 99
24 32 56 99
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32
Merging
24 56 32 99
24 32 56 99
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61
Merging
24 56 32 99
24 32 56 99
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61
Merging
24 56 32 99 9 61
24 32 56 99
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
Merging
24 56 32 99 9 61
24 32 56 99
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
Merging
24 56 32 99 9 61 57 79
24 32 56 99
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
Merging
24 56 32 99 9 61 57 79
24 32 56 99 9 57 61 79
Merge sort
Dividing
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
56 24 99 32 9 61 57 79
Merging
24 56 32 99 9 61 57 79
24 32 56 99 9 57 61 79
9 24 32 56 57 61 79 99
Merge sort complexity
Fast algorithm: worst-case running time grows slowly with input size
Many algorithms exist: bubble sort, insertion sort, quick sort, radix
sort, heap sort, ...
I Useful for developing algorithmic thinking – eg randomized
algorithms
Sorting is a canonical algorithms problem
Many algorithms exist: bubble sort, insertion sort, quick sort, radix
sort, heap sort, ...
I Useful for developing algorithmic thinking – eg randomized
algorithms
Many algorithms exist: bubble sort, insertion sort, quick sort, radix
sort, heap sort, ...
I Useful for developing algorithmic thinking – eg randomized
algorithms
Many algorithms exist: bubble sort, insertion sort, quick sort, radix
sort, heap sort, ...
I Useful for developing algorithmic thinking – eg randomized
algorithms