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]
http://www.cs.cornell.edu/courses/cs1110/2018sp
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:
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
2
3
4
5
6
? (unknown values) 0 n PRE: b sorted 0 n POST: b sorted ? (unknown) 0 k n INV: b
2 4 4 6 6 7 5 0 k 2 4 4 5 6 6 7 0 k
7
8
2 4 4 6 6 7 5 0 k 2 4 4 6 6 5 7 0 k 2 4 4 6 5 6 7 0 k 2 4 4 5 6 6 7 0 k k=6 k=5 k=4 k=3
9
10
11
12
https://www.youtube.com/watch?v=xxcpvCGrCBc
13
14
pre: b
?
h k
x
post: b
<= x >= x
h i i+1 k
x
3 5 4 1 6 2 3 8 1
b h k
change: into
1 2 1 3 5 4 6 3 8
b h i k
15
pre: b
?
h k
x
post: b
<= x >= x
h i i+1 k
x
INV: b
<= x >= x
h i i+1 j k
x ?
16
pre: b
?
h k
x
post: b
<= x >= x
h i i+1 k
x
INV: b
<= x >= x
h i i+1 j k
x ?
17
pre: b
?
h k
x
post: b
<= x >= x
h i i+1 k
x
INV: b
<= x >= x
h i i+1 j k
x ?
18
def partition(b, h, k): i = h j = k+1 x = b[h] 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 return i
pre: b
?
h k
x
INV: b
<= x >= x
h i i+1 j k
x ?
post: b
<= x >= x
h i i+1 k
x
19
def partition(b, h, k): i = h j = k+1 x = b[h] 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 return i
pre: b
?
h k
x
INV: b
<= x >= x
h i i+1 j k
x ?
post: b
<= x >= x
h i i+1 k
x
20
21
?
0 n 0 n b
Sorted!
0 n b <= x x >= x b
x https://www.youtube.com/watch?v=m1PS8IR6Td0
def quick_sort(b, h, k): """Sort the array fragment b[h..k]""" if k<=h: return i = partition(b, h, k) # INV: b[h..i–1] <= b[i] <= b[i+1..k] # Sort b[h..i–1] and b[i+1..k] quick_sort (b, h, i–1) quick_sort (b, i+1, k)
22
x ? h k pre: b <= x x >= x h i i+1 k post: b