Docsity

Invariants, Sequence Algorithms - Introduction to Computing Using Python - Lecture Slides, Slides of Introduction to Computing

Partial preview of the text

Download Invariants, Sequence Algorithms - Introduction to Computing Using Python - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!

Invariants: Keep in mind

• At heart, an invariant is just a way to document

what you want your variables to mean.

This is why you want your code to keep the invariant true;

you want to keep things consistent in your program, and in

your head.

• In our notation, both

b[i+1..i] and

b[i..i-1]

denote an empty sequence.

4/9/

Linear search in unsorted lists

Goal : Given unsorted list b, search range h..k-1 for k >=h and h and k valid indices for b, and target value v, return index n of v's first occurrence in b[h..k-1] (-1 if not found) Restated as postcondition : if n=-1, then v is not in b[h..k-1]. Otherwise, v = b[n] and v is not in b[h..n-1]. Idea : keep an index i, marking position of next thing unchecked; everything to its left has been verified to not be v. If i < k and b[i] = v, return i as n; if i==k, v isn't in b. v not here? h i k inv : b 4/9/

Binary search in sorted lists

Goal : Given sorted list b, search range h..k for k >=h and h and k valid indices for b, and target value v, return index n of v's first occurrence in b[h.. k ] (-1 if not found) Restated as postcondition : if n=-1, then v is not in b[h.. k ]. Otherwise, v = b[n] and v is not in b[h..n-1]. Idea : keep indices i and j, marking position of next thing not known to be < v, and the first thing known to be >=v. Check halfway btwn 'em. If i <= k and b[i] = v, return i as n; if i> k or i ==j and b[i] not v or i=k+1, v isn't in b. =v h i j k inv : b 4/9/

(most of) Binary search implementation

=v h i j k inv : b def bin_search(b,h,k,v): # omitting the last return for space """(see previous)""" Q1: (A) i = h; j=k (B) i=h-1; j = k+1 (C) i = h-1; j = k (D) i = h; j = k+

inv: b[h..i-1] < v, b[j..k] >=v, i <= j; start: b[h..h-1] < v, b[k+1..k] >= v

while Q2: (A) i==j (B) i < j (C) i <=j if b[i] == v: return i mid = (i+j)/ if b[mid] < v: Q3: (A) i = mid (B) i = mid+ else: j = mid # may skip vast section of b 4/9/

Sorting: Selection Sort

? 0 n pre: b (^) sorted 0 n post: b sorted 0 i n inv: b (^)?

Insertion Sort:

INITIALIZE AND COMPLETE

while ...:

j is min item in b[i..n-1]

j = i + b[i:n] .index(min(b[i:n])) sorted, ≤ b[i..] 0 i n inv: b (^) ≥ b[0..i-1] or? if i = 0

Selection Sort:

2 4 4 6 6 8 9 9 7 8 9 i n 2 4 4 6 6 7 9 9 8 8 9 i n 2 4 4 6 6 7 9 9 8 8 9 i n Note the swap of the reds 4/9/

Sorting: Selection Sort

Insertion Sort:

i=

while i < n:

j = = i + b[i:n].index(min(b[i:n]))

b[i], b[j] = b[j], b[i]

i = i + 1

sorted, ≤ b[i..] 0 i n inv: b (^) ≥ b[0..i-1] or? if i = 0

Selection Sort:

4/9/

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

return4/9/13 (i, j)

Dutch National Flag Algorithm

def dnf(b, h, k): """Returns: partition points as a tuple (i,j)""" t = h; i = k+1, j = k;

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: b[i-1], b[t] = b[t], b[i-1] t = t+ elif b[i-1] == 0: i = i- else: b[-1], b[j] = b[j], b[i- i = i-1; j = j-

post: b[h..i-1] < 0, b[i..j] = 0, b[j+1..k] > 0

return (i, j) -1 -2 3 -1 0 0 0 6 3 h t i j k -1 -2 3 -1 0 0 0 6 3 h t i j k < 0? = 0 > 0 -1 -2 -1 3 0 0 0 6 3 h t i j k -1 -2 -1 0 0 0 3 6 3 h t j k 4/9/