lecture 26 sorting
play

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python - PowerPoint PPT Presentation

http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 26: Sorting CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] Announcements Academic Integrity:


  1. http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 26: Sorting CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

  2. Announcements • Academic Integrity: § Remember to cite your sources • Assignment 5 § Due 11:59pm on ***Wednesday*** May 9 th • Last Lab due next week by 10 mins into your Lab Section. • Final Exam § May 17 th , 9am-11:30am § Location : Barton Hall Central and East § Conflict assignment on CMS 2

  3. Announcements • Assignment 5 and Print Statements • Where did your debugging print statements go? § Line 398 in a5_unochecks.py says: # Comment out this line to allow print statements So please go comment this out if you want your print statements to show 3

  4. Plan of Attack • Insertion Sort • Partition • Quick Sort 4

  5. Searching is a good motivation for Sorting Example: 500 CS 1110 Prelims have been scanned Grading Session: “Hey, this scan is hard to read.” Task: go through 500 Exams, find the bad scan Do you want this job? Are the exams in any order? No…. Fine, go through them all. 10 minutes later “Hey, this scan is hard to read…” Now you really wish they were in order… 5

  6. Sorting: Arranging in Ascending Order #1 Insertion K 9 2 7 3 K 9 Sort 3 7 2 #4 #2 K 9 2 7 3 9 K 3 7 2 #5 K 9 3 7 9 3 #3 7 2 K 2 #6 6

  7. Insertion Sort 0 n ? (unknown values) PRE: b k = 0 0 k n INV: b sorted ? (unknown) 0 k while k < n: 2 4 4 6 6 7 5 # Push b[k] down into its # sorted position in b[0..k] 0 k 2 4 4 5 6 6 7 k = k+1 0 n POST: b sorted 7

  8. Insertion Sort: Moving into Position def push_down(b, k): 0 k 2 4 4 6 6 7 5 while k > 0: k=6 if b[k-1] > b[k]: 0 k swap(b,k-1,k) k=5 2 4 4 6 6 5 7 k = k-1 0 k k = 0 k=4 2 4 4 6 5 6 7 while k < n: push_down(b,k) 0 k k=3 2 4 4 5 6 6 7 k = k+1 8

  9. The Importance of Helper Functions def push_down(b, k): Can you understand k = 0 all this code below? while k > 0: while k < n: if b[k-1] > b[k]: j = k swap(b,k-1,k) while j > 0: k = k-1 VS if b[j-1] > b[j]: temp = b[j] k = 0 b[j] = b[j-1] while k < n: b[j-1] = temp push_down(b,k) j = j -1 k = k+1 k = k +1 Also: Is this how you want to sort 500 exams? 9

  10. Algorithm Complexity Nested loops multiply the def push_down(b, k): number of operations required. while k > 0: We need to compare b[k] to all if b[k-1] > b[k]: elements. ~ n operations swap(b,k-1,k) k = k-1 k = 0 Iterating through a sequence of while k < n : length n requires n operations: push_down(b,k) push_down called n times k = k+1 10

  11. Q: Algorithm Complexity def push_down(b, k): Approximately how many operations does this take? while k > 0: if b[k-1] > b[k]: A: ~ 1 operation swap(b,k-1,k) B: ~ n operations k = k-1 C: ~ n 2 operations D: ~ n 3 operations k = 0 E: I don’t know while k < n : push_down(b,k) k = k+1 11

  12. A: Algorithm Complexity def push_down(b, k): Approximately how many operations does this take? while k > 0: if b[k-1] > b[k]: A: ~ 1 operation swap(b,k-1,k) B: ~ n operations k = k-1 C: ~ n 2 operations CORRECT D: ~ n 3 operations k = 0 E: I don’t know while k < n : Total Swaps: 0 + 1 + 2 + 3 + … (n-1) push_down(b,k) = (n-1)*n/2 k = k+1 Insertion sort requires n*n operations 12 https://www.youtube.com/watch?v=xxcpvCGrCBc

  13. Plan of Attack • Insertion Sort • Partition • Quick Sort 13

  14. Partition Algorithm • Given a list segment b[h..k] with some pivot value x in b[h] : h k pre: b x ? • Swap elements of b[h..k] and store in i to satisfy postcondition: h i i+1 k post: b <= x >= x x x Example: § Called the pivot value h k § not a variable change: 3 5 4 1 6 2 3 8 1 b = whatever value is in b[h] h i k into b 1 2 1 3 5 4 6 3 8 14

  15. Partition: What’s the Invariant? • Given a list segment b[h..k] with some pivot value x in b[h] : h k pre: b x ? h i i+1 j k INV: b ? <= x >= x x • Swap elements of b[h..k] and store in i to satisfy postcondition: h i i+1 k post: b <= x >= x x 15

  16. Partition: What’s the Invariant? • Given a list segment b[h..k] with some pivot value x in b[h] : h k pre: b x ? Initially i = h, j = k+1 h i i+1 j k INV: b ? <= x >= x x • Swap elements of b[h..k] and store in i to satisfy postcondition: h i i+1 k post: b <= x >= x x 16

  17. Partition: What’s the Invariant? • Given a list segment b[h..k] with some pivot value x in b[h] : h k pre: b x ? h i i+1 j k INV: b ? <= x >= x x Eventually j = i+1 • Swap elements of b[h..k] and store in i to satisfy postcondition: h i i+1 k post: b <= x >= x x 17

  18. Partition Algorithm Implementation h k pre: b x ? def partition(b, h, k): i = h j = k+1 x = b[h] h i i+1 j k INV: b <= x >= x x ? while i < j-1: if b[i+1] >= x : # Move b[i+1] to end of block. swap(b,i+1,j-1) j = j - 1 else: # b[i+1] < x swap(b,i,i+1) i = i + 1 h i i+1 k post: b <= x >= x x 18 return i

  19. Partition Algorithm Implementation h k pre: b x ? def partition(b, h, k): i = h j = k+1 x = b[h] h i i+1 j k INV: b <= x >= x x ? while i < j-1: if b[i+1] >= x : # Move to end of block. swap(b,i+1,j-1) j = j - 1 else: # b[i+1] < x swap(b,i,i+1) i = i + 1 h i i+1 k post: b <= x >= x x 19 return i

  20. Plan of Attack • Insertion Sort • Partition • Quick Sort 20

  21. Sorting with Partitions 0 n b ? x • Idea: Pick a pivot element x • Partition sequence into <= x and >= x 0 n b <= x x >= x Now Partition this and this, too Keep recursing… 0 n Sorted! b 21 https://www.youtube.com/watch?v=m1PS8IR6Td0

  22. QuickSort def quick_sort ( b, h, k ): """Sort the array fragment b[h..k]""" if k<=h: h k return pre: b x ? i = partition(b, h, k) h i i+1 k post: b # INV: b[h..i–1] <= b[i] <= b[i+1..k] <= x x >= x # Sort b[h..i–1] and b[i+1..k] quick_sort (b, h, i–1) quick_sort (b, i+1, k) 22

Recommend


More recommend