Download Sequence Algorithms - Introduction to Computing Using Python - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!
Sorting: A Key Algorithmic Family
Q: Given a list of items, how can we arrange for them
to be sorted in increasing order , in a time- and
space-efficient manner?
Applications: making items easier to find.
def sort(b, h, k):
""Sort b[h..k] in place. Pre: b: list of ints; k>=h-1"""
# Start with b[h], and organize the rest according to it??
# Note: we have h & k explicit to simplify recursive
# structure.
1 Also, computing poker-hand scores.
Motivation: A Famous Sorting Function
def qsort(b, h, k):
"""Make b[h..k] sorted.
Pre: b: list of ints; k>=h-1"""
i = partition(b, h, k)
qsort(b,0,i-1)
qsort(b,i+1,k)
def partition(b, h, k):
"""Let x = b[h] be the pivot
value. Rearrange b[h..k] so
that there is an i where
b[h..i-1] <= x, b[i]=x; b[i+1..k]
>=x. Return i.
Pre: k>=h"""
# Can you do this without
# creating extra lists?
Clicker Q2: base case Clicker Q1: recursive case
Partition Algorithm
- Given a sequence b[h..k] with some value x in b[h]:
- Swap elements of b[h..k] and store in i to truthify postcondition: or... b 3 5 4 1 6 2 3 8 1 h k change: into b 1 2 1 3 5 4 6 3 8 h i k b 1 2 3 1 3 4 5 6 8 h i k or
- x is called the pivot value x is not a program variable, but a standin for a number: value initially in b[h] x? h k pre: b <= x x >= x h i i+1 k post : b
Motivation: A Famous Sorting Function
def qsort(b, h, k):
"""Make b[h..k] sorted.
Pre: b: list of ints; k>=h-1"""
if k < h: # empty is sorted
return
i = partition(b, h, k)
qsort(b,h,i-1)
qsort(b,i+1,k)
def partition(b, h, k):
"""Let x = b[h] be the pivot
value. Rearrange b[h..k] so
that there is an i where
b[h..i-1] <= x, b[i]=x; b[i+1..k]
>=x. Return i.
Pre: k>=h"""
# Can you do this in place,
# i.e., w/out
# creating extra lists?
Clicker Q2: base case Clicker Q1: recursive case
Partition Algorithm Implementation
def partition(b, h, k): """Partition list b[h..k] around a pivot x = b[h]; Return index of pivot point. Assume a swap function _swap(b,ind1, ind2). Pre: k>=h""" **CLICKER Q
invariant: b[h..i-1] < x, b[i] = x, b[j..k] >= x, b[i+1..j-1] unknown**
while CLICKER Q if b[i+1] >= x:
Move to end of block.
b[i+1], b[j-1] = b[j-1], b[i+1] j = j - 1 else: # b[i+1] < x CLICKER Q
post: b[h..i-1] < x, b[i] is x, and b[i+1..k] >= x
return i
Partition Algorithm Implementation
def partition(b, h, k): """Partition list b[h..k] around a pivot x = b[h]""" i = h; j = k+1; x = b[h]
invariant: b[h..i-1] < x, b[i] = x, b[j..k] >= x
while i < j-1: if b[i+1] >= x:
Move to end of block.
b[i+1], b[j-1] = b[j-1], b[i+1] j = j - 1 else: # b[i+1] < x b[i], b[i+1] = b[i+1], b[i] i = i + 1
post: b[h..i-1] < x, b[i] is x, and b[i+1..k] >= x
return i
h i i+1 j k <= x x? >= x 1 2 1 3 5 0 6 3 8 h i i+1 j k 1 2 1 3 0 5 6 3 8 h i j k 1 2 1 0 3 5 6 3 8 h i j k
Famous "Sort-Like" Example
• Dutch national flag: tri-color
Sequence of h..k of red (<0), white (=0), blue (>0) "pixels" Arrange to put <0 first, then =0 , then >0, return "split pts" ? h k pre: b <0 =0 > h k post: b (values in h..k are unknown) inv : b <0? =0 > h t i j k b[h..t-1] <0, b[t..i-1] unknown, b[i..j] =0, b[j+1..k] >
Dutch National Flag Algorithm
def dnf(b, h, k): """(DNF explanation omitted for space.) Returns: split-points as a tuple (i,j)"""
init?
# inv: b[h..t-1] < 0, b[t..i-1] ?, b[i..j] = 0, b[j+1..k] > 0 while t < i: if b[i-1] < 0:
what?
elif b[i-1] == 0:
what?
else:
what?
post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0
return (i, j)
h t i j k -1 -2 3 -1 0 0 0 6 3 h t i j k
h t i j k -1 -2 -1 0 0 0 3 6 3 h i j k h t j k