Lecture 27 Sorting
Announcements for This Lecture Finishing Up Assignment 7 • Submit a course evaluation • Should be on bolt collisions § Will get an e-mail for this • Use weekend for final touches § Part of “participation grade” § Multiple lives • Final: Dec 17 th 9-11:30am § Winning or losing the game § Study guide is posted • Also work on the extension § Announce reviews on Tues. § Add anything you want • Conflict with Final time? § ONLY NEED ONE § Submit to conflict to CMS § Ask on Piazza if unsure by next Tuesday! § All else is extra credit 12/5/19 Sorting 2
Linear Search • Vague : Find first occurrence of v in b[h..k-1]. 12/5/19 Sorting 3
Linear Search • Vague : Find first occurrence of v in b[h..k-1]. • Better : Store an integer in i to truthify result condition post: post: 1. v is not in b[h..i-1] 2. i = k OR v = b[i] 12/5/19 Sorting 4
Linear Search • Vague : Find first occurrence of v in b[h..k-1]. • Better : Store an integer in i to truthify result condition post: post: 1. v is not in b[h..i-1] 2. i = k OR v = b[i] h k pre: b ? h i k post: b v not here v ? 12/5/19 Sorting 5
Linear Search • Vague : Find first occurrence of v in b[h..k-1]. • Better : Store an integer in i to truthify result condition post: post: 1. v is not in b[h..i-1] 2. i = k OR v = b[i] h k pre: b ? h i k post: b v not here v ? OR i h k b v not here 12/5/19 Sorting 6
Linear Search h k pre: b ? h i k post: b v not here v ? OR i h k b v not here h i k inv: b v not here ? 12/5/19 Sorting 7
Linear Search def linear_search(b,v,h,k): Analyzing the Loop """Returns: first occurrence of v in b[h..k-1]""" 1. Does the initialization # Store in i index of the first v in b[h..k-1] make inv true? i = h 2. Is post true when inv is true and condition is false? # invariant: v is not in b[h..i-1] while i < k and b[i] != v: 3. Does the repetend make progress? i = i + 1 4. Does the repetend keep the # post: v is not in b[h..i-1] invariant inv true? # i >= k or b[i] == v return i if i < k else -1 12/5/19 Sorting 8
Binary Search • Vague: Look for v in sorted sequence segment b[h..k]. 12/5/19 Sorting 9
Binary Search • Vague: Look for v in sorted sequence segment b[h..k]. • Better: § Precondition: b[h..k-1] is sorted (in ascending order). § Postcondition: b[h..i-1] < v and v <= b[i..k] • Below, the array is in non-descending order: h k pre: b ? sorted h i k post: b < v >= v 12/5/19 Sorting 10
Binary Search • Look for value v in sorted segment b[h..k] h k pre: b ? New statement of the invariant guarantees h i k that we get leftmost post: b < v >= v position of v if found h i j k inv: b >= v < v ? § if v is 3, set i to 0 h k § if v is 4, set i to 5 0 1 2 3 4 5 6 7 8 9 § if v is 5, set i to 7 Example b 3 3 3 3 3 4 4 6 7 7 § if v is 8, set i to 10 12/5/19 Sorting 11
Binary Search • Vague: Look for v in sorted sequence segment b[h..k]. • Better: § Precondition: b[h..k-1] is sorted (in ascending order). § Postcondition: b[h..i-1] < v and v <= b[i..k] • Below, the array is in non-descending order: h k pre: b ? Called binary search h i k because each iteration post: b < v >= v of the loop cuts the h i j k array segment still to be processed in half inv: b > v < v ? 12/5/19 Sorting 12
Binary Search h k pre: b ? New statement of the h i k invariant guarantees post: b < v >= v that we get leftmost position of v if found h i j k inv: b >= v < v ? i = h; j = k+1; while i != j: Looking at b[i] gives linear search from left. Looking at b[j-1] gives linear search from right. Looking at middle: b[(i+j)/2] gives binary search. 12/5/19 Sorting 13
Sorting: Arranging in Ascending Order 0 n 0 n pre: b post: b ? sorted Insertion Sort: 0 i n inv: b ? sorted i = 0 0 i while i < n: 2 4 4 6 6 7 5 # Push b[i] down into its 0 i # sorted position in b[0..i] 2 4 4 5 6 6 7 i = i+1 12/5/19 Sorting 14
Insertion Sort: Moving into Position i = 0 0 i while i < n: 2 4 4 6 6 7 5 push_down(b,i) i = i+1 def push_down(b, i): j = i while j > 0: swap shown in the if b[j-1] > b[j]: lecture about lists swap(b,j-1,j) j = j-1 12/5/19 Sorting 15
Insertion Sort: Moving into Position i = 0 0 i while i < n: 2 4 4 6 6 7 5 push_down(b,i) i = i+1 0 i 2 4 4 6 6 5 7 def push_down(b, i): j = i while j > 0: swap shown in the if b[j-1] > b[j]: lecture about lists swap(b,j-1,j) j = j-1 12/5/19 Sorting 16
Insertion Sort: Moving into Position i = 0 0 i while i < n: 2 4 4 6 6 7 5 push_down(b,i) i = i+1 0 i 2 4 4 6 6 5 7 def push_down(b, i): j = i 0 i while j > 0: 2 4 4 6 5 6 7 swap shown in the if b[j-1] > b[j]: lecture about lists swap(b,j-1,j) j = j-1 12/5/19 Sorting 17
Insertion Sort: Moving into Position i = 0 0 i while i < n: 2 4 4 6 6 7 5 push_down(b,i) i = i+1 0 i 2 4 4 6 6 5 7 def push_down(b, i): j = i 0 i while j > 0: 2 4 4 6 5 6 7 swap shown in the if b[j-1] > b[j]: lecture about lists 0 i swap(b,j-1,j) 2 4 4 5 6 6 7 j = j-1 12/5/19 Sorting 18
The Importance of Helper Functions Can you understand i = 0 i = 0 all this code below? while i < n: while i < n: push_down(b,i) j = i i = i+1 while j > 0: VS if b[j-1] > b[j]: def push_down(b, i): j = i temp = b[j] while j > 0: b[j] = b[j-1] if b[j-1] > b[j]: b[j-1] = temp swap(b,j-1,j) j = j -1 j = j-1 i = i +1 12/5/19 Sorting 19
Insertion Sort: Performance def push_down(b, i): • b[0..i-1]: i elements """Push value at position i into • Worst case: sorted position in b[0..i-1]""" § i = 0: 0 swaps j = i § i = 1: 1 swap while j > 0: § i = 2: 2 swaps if b[j-1] > b[j]: • Pushdown is in a loop swap(b,j-1,j) § Called for i in 0..n j = j-1 § i swaps each time Total Swaps: 0 + 1 + 2 + 3 + … (n-1) = (n-1)*n/2 = (n 2 -n)/2 12/5/19 Sorting 20
Insertion Sort: Performance def push_down(b, i): • b[0..i-1]: i elements """Push value at position i into • Worst case: sorted position in b[0..i-1]""" § i = 0: 0 swaps j = i § i = 1: 1 swap while j > 0: § i = 2: 2 swaps if b[j-1] > b[j]: • Pushdown is in a loop swap(b,j-1,j) § Called for i in 0..n j = j-1 § i swaps each time Insertion sort is an n 2 algorithm Total Swaps: 0 + 1 + 2 + 3 + … (n-1) = (n-1)*n/2 = (n 2 -n)/2 12/5/19 Sorting 21
Algorithm “Complexity” • Given : a list of length n and a problem to solve • Complexity : rough number of steps to solve worst case • Suppose we can compute 1000 operations a second: Complexity n=10 n=100 n=1000 n 0.01 s 0.1 s 1 s n log n 0.016 s 0.32 s 4.79 s n 2 0.1 s 10 s 16.7 m n 3 1 s 16.7 m 11.6 d 4x10 19 y 3x10 290 y 2 n 1 s Major Topic in 2110: Beyond scope of this course 12/5/19 Sorting 22
Sorting: Changing the Invariant 0 n 0 n pre: b post: b ? sorted Selection Sort: Insertion Sort: 0 i 0 i n n First segment always inv: b inv: b contains smaller values ≥ b[0..i-1] ? sorted, ≤ b[i..] sorted i n i = 0 2 4 4 6 6 8 9 9 7 8 9 while i < n: i n # Find minimum in b[i..] 2 4 4 6 6 7 9 9 8 8 9 # Move it to position i i n i = i+1 2 4 4 6 6 7 9 9 8 8 9 12/5/19 Sorting 23
Sorting: Changing the Invariant 0 n 0 n pre: b post: b ? sorted Insertion Sort: Selection Sort: 0 i 0 i n n First segment always inv: b inv: b contains smaller values ≥ b[0..i-1] ? sorted, ≤ b[i..] sorted Compared to insertion sort, i = 0 selection sort is while i < n: A: Slower # Find minimum in b[i..] B: About the same # Move it to position i C: Faster i = i+1 D: I don’t know 12/5/19 Sorting 24
Recommend
More recommend