CS 1110: Introduction to Computing Using Pyton Lecture 23 Sorting and Searcing [Andersen, Gries, Lee, Marscner, Van Loan, Wite]
Announcements Final Exam conflicts due tonigt at 11:59pm Final Exam review sessions on te 14 t Labs on 5/9 and 5/10 will be office ours Assignment 5 Due 11:59pm on ***Wednesday*** May 10 t Lab 13 is out 5/2/17 Sorting and Searcing 2
Recall: Accessing te Original Metod Wat if you want to use te original version metod? New metod = original+more Do not want to repeat code from te original version Call old metod explicitly Use metod as a function Pass object as first argument Example: Employee. str (self) class Employee(object): """An Employee wit a salary""" def str (self): return (self._name + ', year ' + str(self._start) + ', salary ' + str(self._salary)) class Executive(Employee): """An Employee wit a bonus.""" def str (self): return (Employee. str (self) + ', bonus ' + str(self._bonus)) 5/2/17 Sorting and Searcing 3
super Can also use super super(<class>, <instance>) returns te parent class of <class> and <instance> Example: super(executive, self). str () class Employee(object): """An Employee wit a salary""" def str (self): return (self._name + ', year ' + str(self._start) + ', salary ' + str(self._salary)) class Executive(Employee): """An Employee wit a bonus.""" def str (self): return (super(executive, self). str () + ', bonus ' + str(self._bonus)) 5/2/17 Sorting and Searcing 4
Dutc National Flag Variant Sequence of integer values red = negatives, wite = 0, blues = positive Only rearrange part of te list, not all pre: b? post: b < 0 = 0 > 0 t i j inv: b < 0? = 0 > 0 5/2/17 Sorting and Searcing 5
Dutc National Flag Variant Sequence of integer values red = negatives, wite = 0, blues = positive Only rearrange part of te list, not all pre: b? post: b < 0 = 0 > 0 t i j inv: b < 0? = 0 > 0 pre: t =, i = +1, j = post: t = i 5/2/17 Sorting and Searcing 6
Dutc National Flag Algoritm def dnf(b,, ): """Returns: partition points as a tuple (i,j)""" t = ; i = +1, j = ; # inv: b[..t-1] < 0, b[t..i-1]?, b[i..j] = 0, b[j+1..] > 0 wile t < i: if b[i-1] < 0: swap(b,i-1,t) t = t+1 elif b[i-1] == 0: i = i-1 else: swap(b,i-1,j) i = i-1 j = j-1 # post: b[..i-1] < 0, b[i..j] = 0, b[j+1..] > 0 return (i, j) 5/2/17 Sorting and Searcing < 0? = 0 > 0 t i j -1-2 3-1 0 0 0 6 3 t i j -1-2 3-1 0 0 0 6 3 t i j -1-2 -1 3 0 0 0 6 3 i t j -1-2 -1 0 0 0 3 6 3 7
Partition Algoritm Given a list segment b[..] wit some pivot value x in b[]: pre: b Swap elements of b[..] and store in i to trutify post: post: b cange: b x? i i+1 <= x x >= x 3 5 4 1 6 2 3 8 1 i into b 1 2 1 3 5 4 6 3 8 5/2/17 Sorting and Searcing 8
Sorting wit Partitions Given a list segment b[..] wit some value x in b[]: pre: b x? Swap elements of b[..] and store in j to trutify post: i i+1 post: b y <= y <= y? x >= y x >= x Partition Recursively Recursive partitions = sorting 5/2/17 Sorting and Searcing 11
QuicSort def quic_sort(b,, ): """Sort te array fragment b[..]""" if b[..] as fewer tan 2 elements: return i = partition(b,, ) # b[..i 1] <= b[i] <= b[i+1..] # Sort b[..i 1] and b[i+1..] quic_sort (b,, i 1) quic_sort (b, i+1, ) pre: b post: b x? i i+1 <= x x >= x 5/2/17 Sorting and Searcing 12
Linear Searc Vague: Find first occurrence of v in b[..-1]. Better: Store an integer in i to trutify result condition post: post: 1. v is not in b[..i-1] 2. i = OR v = b[i] 5/2/17 Sorting and Searcing 19
Linear Searc Vague: Find first occurrence of v in b[..-1]. Better: Store an integer in i to trutify result condition post: post: 1. v is not in b[..i-1] 2. i = OR v = b[i] pre: b? post: b i v not ere v? 5/2/17 Sorting and Searcing 20
Linear Searc Vague: Find first occurrence of v in b[..-1]. Better: Store an integer in i to trutify result condition post: post: 1. v is not in b[..i-1] 2. i = OR v = b[i] pre: b? post: b i v not ere v? OR b v not ere i 5/2/17 Sorting and Searcing 21
pre: b Linear Searc? post: b i v not ere v? OR b v not ere i inv: b i v not ere? 5/2/17 Sorting and Searcing 22
Linear Searc def linear_searc(b,v,,): """Returns: first occurrence of v in b[..-1]""" # Store in i index of te first v in b[..-1] i = # invariant: v is not in b[0..i-1] wile i < and b[i]!= v: i = i + 1 # post: v is not in b[..i-1] # i >= or b[i] == v return i if i < else -1 Analyzing te Loop 1. Does te initialization mae inv true? 2. Is post true wen inv is true and condition is false? 3. Does te repetend mae progress? 4. Does te repetend eep te invariant inv true? 5/2/17 Sorting and Searcing 23
Binary Searc Loo for v in sorted sequence segment b[..]. 5/2/17 Sorting and Searcing 24
Binary Searc Loo for v in sorted sequence segment b[..]. Precondition: b[..-1] is sorted (in ascending order). Postcondition: b[..i-1] < v and v <= b[i..] pre: b? post: b i < v >= v 5/2/17 Sorting and Searcing 25
Binary Searc Loo for value v in sorted segment b[..] pre: b post: b inv: b < v? i < v >= v i j? >= v Called binary searc because eac iteration of te loop cuts te array segment still to be processed in alf Example b 0 1 2 3 4 5 6 7 8 9 3 3 3 3 3 4 4 6 7 7 if v is 3, set i to 0 if v is 4, set i to 5 if v is 5, set i to 7 if v is 8, set i to 10 5/2/17 Sorting and Searcing 26
Binary Searc pre: b? i post: b < v >= v i j inv: b < v? >= v i = ; j = +1; wile i!= j: Looing at b[i] gives linear searc from left. Looing at b[j-1] gives linear searc from rigt. Looing at middle: b[(i+j)/2] gives binary searc. 5/2/17 Sorting and Searcing 27
Binary Searc def bsearc(b, v): i = 0 j = len(b) # invariant; b[0..i-1] < v, b[i..j-1] unnown, b[j..] >= v wile i < j: mid = (i+j)/2 if b[mid] < v: i = mid+1 else: #b[mid] >= v j = mid if i< len(b) and b[i] == v: else: return i return -1 Analyzing te Loop 1. Does te initialization mae inv true? 2. Is post true wen inv is true and condition is false? 3. Does te repetend mae progress? 4. Does te repetend eep te invariant inv true? 5/2/17 Sorting and Searcing 28
Binary Searc Recursive def rbsearc(b, v): """ len(b) > 0 """ return rbsearc_elper(b, v, 0, len(b)) def rbsearc_elper(b, v, i, j): if i >= j: if i < len(b) and b[i] == v: return i else: return -1 mid = (i + j) / 2 if b[mid] < v: return rbsearc_elper(b, v, mid + 1, j) else: # b[mid] >= v return rbsearc_elper(b, v, i, mid) 5/2/17 Sorting and Searcing 29