Developing Loops from Invariants

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
1 CS March 2009 While loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim in two days: Th 7:30-9pm Uris Aud.
1 CS100J 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
1 CS100J 29 March 2007 Arrays: searching & sorting. Reading: 8.5 Why did I get a Christmas Card on Halloween? Searching and sorting algorithms are on the.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Reading and Writing Mathematical Proofs
Recitation 11 Analysis of Algorithms and inductive proofs 1.
1 CS March 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
1 CS November 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 Assertions. 2 A boolean expression or predicate that evaluates to true or false in every state In a program they express constraints on the state that.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS April 2009 Sorting: insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
1 CS100J 06 March 2008 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
1 CS April 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
Chapter 3 of Programming Languages by Ravi Sethi
SWEN421 – Lecture 4 Contracts and Correctness
EGR 2261 Unit 10 Two-dimensional Arrays
Correctness issues and Loop invariants
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Data Structures in Java with JUnit ©Rick Mercer
CS October 2009 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
LOOPS.
Some Basics for Problem Analysis and Solutions
Arrays & Functions Lesson xx
Developing Loops from Invariants
Review 4 Lists and Sequences.
CS 220: Discrete Structures and their Applications
Algorithm Discovery and Design
"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne Sorting Lecture 11 CS2110 – Fall 2018.
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
CEV208 Computer Programming
Lecture 13: Two-Dimensional Arrays
Building Java Programs
"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne Sorting Lecture 11 CS2110 – Spring 2019.
Binary Search and Loop invariants
Asymptotic complexity
CS November 2010 Developing array algorithms. Reading:
Sorting and Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Searching and Sorting Hint at Asymptotic Complexity
Developing loops from invariants
CS100J Nov 04, 2003 Arrays. Reading: 8
Review 4 Lists and Sequences.
EECE.2160 ECE Application Programming
CS100A Sections Dec Loop Invariant Review C Review and Example
COP4020 Programming Languages
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
CS100J 16 Oct, 2007 Assignment A5: loops
CS October 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Presentation transcript:

Developing Loops from Invariants Review 6 Developing Loops from Invariants

Feel free to ask questions at any time Outline Creating loops from invariants What is on the exam Common mistakes Feel free to ask questions at any time

Developing a Loop on a Range of Integers Given a range of integers a..b to process. Possible alternatives Could use a for-loop: for x in range(a,b+1): Or could use a while-loop: x = a; while a <= b: Which one you can use will be specified But does not remove the need for invariants Invariants: properties of variables outside loop (as well as the loop counter x) If body has any variables accessed outside of loop, you need an invariant

Developing an Integer Loop (a) Suppose you are trying to implement the command Process a..b Write the command as a postcondition:      post: a..b has been processed.

Developing an Integer Loop (b) Set-up using for: for k in range(a,b+1): # Process k # post: a..b has been processed.

Developing an Integer Loop (b) Set-up using while: while k <= b: # Process k k = k + 1 # post: a..b has been processed.

Developing an Integer Loop (c) Add the invariant (for): # invariant: a..k-1 has been processed for k in range(a,b+1): # Process k # post: a..b has been processed. Note it is post condition with the loop variable

Developing an Integer Loop (c) Add the invariant (while): # invariant: a..k-1 has been processed while k <= b: # Process k k = k + 1 # post: a..b has been processed. Note it is post condition with the loop variable

Developing a For-Loop (d) Fix the initialization: init to make invariant true # invariant: a..k-1 has been processed for k in range(a,b+1): # Process k # post: a..b has been processed. Nothing to do unless invariant has variables other than loop variable Why did not use loop invariants with for loops

Developing a For-Loop (d) Fix the initialization: init to make invariant true # invariant: a..k-1 has been processed while k <= b: # Process k k = k + 1 # post: a..b has been processed. Has to handle the loop variable (and others)

Developing a For-Loop (e) Figure out how to “Process k”: init to make invariant true # invariant: a..k-1 has been processed for k in range(a,b+1): # Process k implementation of “Process k” # post: a..b has been processed.

Developing a For-Loop (e) Figure out how to “Process k”: init to make invariant true # invariant: a..k-1 has been processed while k <= b: # Process k implementation of “Process k” k = k + 1 # post: a..b has been processed.

Pay attention to range: This affects the loop condition! a..b or a+1..b or a…b-1 or … This affects the loop condition! Range a..b-1, has condition k < b Range a..b, has condition k <= b Note that a..a-1 denotes an empty range There are no values in it

Modified Question 3 from Spring 2008 A magic square is a square where each row and column adds up to the same number (often this also includes the diagonals, but for this problem, we will not). For example, in the following 5-by-5 square, each row and column add up to 70:

def are_magic_rows(square, value): """Returns: True if all rows of square sum to value Precondition: square is a 2d list of numbers""" i = 0 # invariant: each row 0..i-1 sums to value while i < len(square) : # Return False if row i does not sum to value rowsum = 0 # invariant: elements 0..k-1 of square[i] sum to rowsum for k in range(len(square)): # rows == cols rowsum = rowsum + square[i][k] if rowsum != value: return False i = i+1 # invariant: each row 0..len(square)-1 sums to value return True

def are_magic_rows(square, value): """Returns: True if all rows of square sum to value Precondition: square is a 2d list of numbers""" i = 0 # invariant: each row 0..i-1 sums to value while i < len(square) : # Return False if row i does not sum to value rowsum = 0 # invariant: elements 0..k-1 of square[i] sum to rowsum for k in range(len(square)): # rows == cols rowsum = rowsum + square[i][k] if rowsum != value: return False i = i+1 # invariant: each row 0..len(square)-1 sums to value return True

Inner invariant was not required def are_magic_rows(square, value): """Returns: True if all rows of square sum to value Precondition: square is a 2d list of numbers""" i = 0 # invariant: each row 0..i-1 sums to value while i < len(square) : # Return False if row i does not sum to value rowsum = 0 # invariant: elements 0..k-1 of square[i] sum to rowsum for k in range(len(square)): # rows == cols rowsum = rowsum + square[i][k] if rowsum != value: return False i = i+1 # invariant: each row 0..len(square)-1 sums to value return True Inner invariant was not required

Modified Question 4 from Spring 2007 # Given lists b, c, d which with single digit elements # len(b) = len(c) >= len(d) # Want to ‘add’ c and d and put result in b h = ______ k = ______ carry = _______ # invariant: b[h..] contains the sum of c[h..] and d[k..], # except that the carry into position k-1 is in 'carry' while ___________ : # postcondition: b contains the sum of c and d # except that the carry contains the 0 or 1 at the beginning

Modified Question 4 from Spring 2007 # Given lists b, c, d which with single digit elements # len(b) = len(c) >= len(d) # Want to ‘add’ c and d and put result in b h = ______ k = ______ carry = _______ # invariant: b[h..] contains the sum of c[h..] and d[k..], # except that the carry into position k-1 is in 'carry' while ___________ : # postcondition: b contains the sum of c and d # except that the carry contains the 0 or 1 at the beginning c d b d[0] b[0]

Modified Question 4 from Spring 2007 h = len(c) k = len(d) carry = 0 # invariant: b[h..] contains the sum of c[h..] and d[k..], # except that the carry into position k-1 is in 'carry' while h > 0: h = h -1; k = k – 1 # Easier if decrement first x = d[k] if k>= 0 else 0 b[h] = c[h]+x+carry if b[h] >= 10: carry = 1; b[h] = b[h]-10 else: # postcondition: b contains the sum of c and d # except that the carry contains the 0 or 1 at the beginning c d b

Will cost you points on the exam! DOs and DON’Ts #1 DO use variables given in the invariant. DON’T use other variables. # invariant: b[h..] contains the sum of c[h..] and d[k..], # except that the carry into position k-1 is in 'carry' while ___________ : # Okay to use b, c, d, h, k, and carry # Anything else should be ‘local’ to while On the test, we may give them a question where we state the precondition, postcondition, and loop invariant and ask them to write the loop with initialization. When this is done, they MUST use the given invariant. They should NOT add other variables declared outside the loop, although they may declare variables within the repetend. Will cost you points on the exam!

DOs and DON’Ts #2 DO double check corner cases! h = len(c) while h > 0: What will happen when h=1 and h=len(c)? If you use h in c (e.g. c[x]) can you possibly get an error? # invariant: b[h..] contains the sum of c[h..] and d[k..], # except that the carry into position k-1 is in 'carry' while h > 0: … Range is off by 1. How do you know?

Questions?

Review 7 Sequence Algorithms

Three Types of Questions Write body of a loop to satisfy a given invariant. Problem 6, Fall 2013 (Final) Problem 6, Spring 2014 (Final) Given an invariant with code, identify all errors. Problem 6, Spring 2014 (Prelim 2) Problem 6, Spring 2013 (Final) Given an example, rewrite it with new invariant. Problem 8, Fall 2014 (Final)

Horizontal Notation for Sequences b <= sorted >= 0 k len(b) Example of an assertion about an sequence b. It asserts that: b[0..k–1] is sorted (i.e. its values are in ascending order) Everything in b[0..k–1] is ≤ everything in b[k..len(b)–1] Given index h of the first element of a segment and index k of the element that follows that segment, the number of values in the segment is k – h. b[h .. k – 1] has k – h elements in it. b 0 h k h h+1 (h+1) – h = 1

DOs and DON’Ts #3 DON’T put variables directly above vertical line. Where is j? Is it unknown or >= x? <= x x ? >= x h i j k b For that matter, any loop that does this    Process b[h..k] Probably has a loop invariant that looks like this:     h                     i                      k   -------------------------------------- b|   processed |     ?                   |   ------------------------------------- BIG PROBLEM IN DRAWING SUCH DIAGRAMS. DO NOT PUT THE VARIABLE DIRECTLY ABOVE THE VERTICAL LINE. Stress this over and over:           k     -------          THIS IS STUPID! Shows lack of care. It's ambiguous           |     -------             k     -------          Now it labels the first element of the segment           |     -------

Algorithm Inputs We may specify that the list in the algorithm is b[0..len(b)-1] or a segment b[h..k] or a segment b[m..n-1] Work with whatever is given! Remember formula for # of values in an array segment Following – First e.g. the number of values in b[h..k] is k+1–h. ? h k b

Three Types of Questions Write body of a loop to satisfy a given invariant. Problem 6, Fall 2013 (Final) Problem 6, Spring 2014 (Final) Given an invariant with code, identify all errors. Problem 6, Spring 2014 (Prelim 2) Problem 6, Spring 2013 (Final) Given an example, rewrite it with new invariant. Problem 8, Fall 2014 (Final)

Unchanged, values all in b[h+1..k] Exercise 6, Fall 2013 Final sorted 0 k pre: b 0 h k post: b unchanged b[0..k] w/o duplicates inv: b 0 p h k Unchanged, values all in b[h+1..k] unchanged b[p+1..k] w/o duplicates Example: Input [1, 2, 2, 2, 4, 4, 4] Output [1, 2, 2, 2, 1, 2, 4]

Unchanged, values all in b[h+1..k] Solution to Fall 2013 Final inv: b 0 p h k Unchanged, values all in b[h+1..k] unchanged b[p+1..k] w/o duplicates # Assume 0 <= k, so the list segment has at least one element p = h = # inv: b[h+1..k] is original b[p+1..k] with no duplicates # b[p+1..h] is unchanged from original list w/ values in b[h+1..k] # b[0..p] is unchanged from original list while :

Unchanged, values all in b[h+1..k] Solution to Fall 2013 Final inv: b 0 p h k Unchanged, values all in b[h+1..k] unchanged b[p+1..k] w/o duplicates # Assume 0 <= k, so the list segment has at least one element p = k-1 h = k-1 # inv: b[h+1..k] is original b[p+1..k] with no duplicates # b[p+1..h] is unchanged from original list w/ values in b[h+1..k] # b[0..p] is unchanged from original list while :

Unchanged, values all in b[h+1..k] Solution to Fall 2013 Final inv: b 0 p h k Unchanged, values all in b[h+1..k] unchanged b[p+1..k] w/o duplicates # Assume 0 <= k, so the list segment has at least one element p = k-1 h = k-1 # inv: b[h+1..k] is original b[p+1..k] with no duplicates # b[p+1..h] is unchanged from original list w/ values in b[h+1..k] # b[0..p] is unchanged from original list while 0 <= p:

Unchanged, values all in b[h+1..k] Solution to Fall 2013 Final inv: b 0 p h k Unchanged, values all in b[h+1..k] unchanged b[p+1..k] w/o duplicates # Assume 0 <= k, so the list segment has at least one element p = k-1 h = k-1 # inv: b[h+1..k] is original b[p+1..k] with no duplicates # b[p+1..h] is unchanged from original list w/ values in b[h+1..k] # b[0..p] is unchanged from original list while 0 <= p: if b[p] != b[p+1]: b[h] = b[p] h = h-1 p = p-1

Exercise 6, Spring 2014 Final Example: Elements of string s1 0 len(b) pre: b 0 j len(b) post: b Elements in s2 Elements not in s2 inv: b 0 i j len(b) Elts in s2 ??? Elts not in s2 Example: Input s1 = 'abracadabra', s2 = 'abc' Output 'abacaabardr' (or 'aaaabbcrdr')

Solution to Spring 2014 Final # convert to a list b b = list(s1) # initialize counters # inv: b[0..i-1] in s2; b[j+1..n-1] not in s2 while : # post: b[0..j] in s2; b[i+1..n-1] not in s2 # convert b back to a string

Solution to Spring 2014 Final # convert to a list b b = list(s1) # initialize counters i = 0 j = len(b) - 1 # inv: b[0..i-1] in s2; b[j+1..n-1] not in s2 while : # post: b[0..j] in s2; b[i+1..n-1] not in s2 # convert b back to a string Inv: 0 i j len(b) Elts in s2 ??? Elts not in s2

Solution to Spring 2014 Final # convert to a list b b = list(s1) # initialize counters i = 0 j = len(b) - 1 # inv: b[0..i-1] in s2; b[j+1..n-1] not in s2 while j != i - 1: # post: b[0..j] in s2; b[i+1..n-1] not in s2 # convert b back to a string Inv: 0 i j len(b) Elts in s2 ??? Elts not in s2

Solution to Spring 2014 Final # convert to a list b b = list(s1) # initialize counters i = 0 j = len(b) - 1 # inv: b[0..i-1] in s2; b[j+1..n-1] not in s2 while j != i - 1: if b[i] in s2: i = i + 1 else: b[i], b[j] = b[j], b[i] # Fancy swap syntax in python j = j – 1 # post: b[0..j] in s2; b[i+1..n-1] not in s2 # convert b back to a string Inv: 0 i j len(b) Elts in s2 ??? Elts not in s2

Solution to Spring 2014 Final # convert to a list b b = list(s1) # initialize counters i = 0 j = len(b) - 1 # inv: b[0..i-1] in s2; b[j+1..n-1] not in s2 while j != i - 1: if b[i] in s2: i = i + 1 else: b[i], b[j] = b[j], b[i] # Fancy swap syntax in python j = j – 1 # post: b[0..j] in s2; b[i+1..n-1] not in s2 # convert b back to a string result = ''.join(b) Inv: 0 i j len(b) Elts in s2 ??? Elts not in s2

Three Types of Questions Write body of a loop to satisfy a given invariant. Problem 6, Fall 2013 (Final) Problem 6, Spring 2014 (Final) Given an invariant with code, identify all errors. Problem 6, Spring 2014 (Prelim 2) Problem 6, Spring 2013 (Final) Given an example, rewrite it with new invariant. Problem 8, Fall 2014 (Final)

Exercise 6, Spring 2014 Prelim 2 def partition(b, z): i = 1 k = len(b) # inv: b[0..i-1] <= z and b[k..] > z while i != k: if b[i] <= z: i = i + 1 else: k = k–1 b[i], b[k] = b[k], b[i] # python swap # post: b[0..k-1] <= z and b[k..] > z return k <= z 0 i k len(b) inv: b ??? >= z

Exercise 6, Spring 2014 Prelim 2 def partition(b, z): i = 1 i = 0 k = len(b) # inv: b[0..i-1] <= z and b[k..] > z while i != k: if b[i] <= z: i = i + 1 else: k = k–1 b[i], b[k] = b[k], b[i] # python swap # post: b[0..k-1] <= z and b[k..] > z return k <= z 0 i k len(b) inv: b ??? >= z

Exercise 6, Spring 2014 Prelim 2 def partition(b, z): i = -1 k = len(b) # inv: b[0..i] <= z and b[k..] > z while i != k: if b[i+1] <= z: i = i + 1 else: b[i+1], b[k–1] = b[k–1], b[i+1] # python swap k = k–1 # post: b[0..k-1] <= z and b[k..] > z return k <= z 0 i k len(b) inv: b ??? >= z

Exercise 6, Spring 2014 Prelim 2 def partition(b, z): i = -1 k = len(b) # inv: b[0..i] <= z and b[k..] > z while i != k: i != k–1: if b[i+1] <= z: i = i + 1 else: b[i+1], b[k–1] = b[k–1], b[i+1] # python swap k = k–1 # post: b[0..k-1] <= z and b[k..] > z return k <= z 0 i k len(b) inv: b ??? >= z

Exercise 6, Spring 2013 Final def num_space_runs(s): """The number of runs of spaces in the string s. Examples: ' a f g ' is 4 'a f g' is 2 ' a bc d' is 3. Precondition: len(s) >= 1""" i = 1 n = 1 if s[0] == ' ' else 0 # inv: s[0..i] contains n runs of spaces while i != len(s): if s[i] == ' ' and s[i-1] != ' ': n = n+1 i = i+1 # post: s[0..len(s)-1] contains n runs of spaces return n return n

Exercise 6, Spring 2013 Final def num_space_runs(s): """The number of runs of spaces in the string s. Examples: ' a f g ' is 4 'a f g' is 2 ' a bc d' is 3. Precondition: len(s) >= 1""" i = 1 i = 0 n = 1 if s[0] == ' ' else 0 # inv: s[0..i] contains n runs of spaces while i != len(s): if s[i] == ' ' and s[i-1] != ' ': n = n+1 i = i+1 # post: s[0..len(s)-1] contains n runs of spaces return n return n

Exercise 6, Spring 2013 Final def num_space_runs(s): """The number of runs of spaces in the string s. Examples: ' a f g ' is 4 'a f g' is 2 ' a bc d' is 3. Precondition: len(s) >= 1""" i = 1 i = 0 n = 1 if s[0] == ' ' else 0 # inv: s[0..i] contains n runs of spaces while i != len(s): i != len(s)–1 if s[i] == ' ' and s[i-1] != ' ': n = n+1 i = i+1 # post: s[0..len(s)-1] contains n runs of spaces return n return n

Exercise 6, Spring 2013 Final def num_space_runs(s): """The number of runs of spaces in the string s. Examples: ' a f g ' is 4 'a f g' is 2 ' a bc d' is 3. Precondition: len(s) >= 1""" i = 1 i = 0 n = 1 if s[0] == ' ' else 0 # inv: s[0..i] contains n runs of spaces while i != len(s): i != len(s)–1 if s[i] == ' ' and s[i-1] != ' ': s[i+1] == ' ' and s[i] != ' ': n = n+1 i = i+1 # post: s[0..len(s)-1] contains n runs of spaces return n return n

Three Types of Questions Write body of a loop to satisfy a given invariant. Problem 6, Fall 2013 (Final) Problem 6, Spring 2014 (Final) Given an invariant with code, identify all errors. Problem 6, Spring 2014 (Prelim 2) Problem 6, Spring 2013 (Final) Given an example, rewrite it with new invariant. Problem 8, Fall 2014 (Final)

Partition Example # Make invariant true at start j = h t = k+1 # inv: b[h..j–1] <= x = b[j] <= b[t..k] while j < t–1: if b[j+1] <= b[j]: swap b[j] and b[j+1] j = j+1 else: swap b[j+1] and b[t-1] t=t–1 # post: b[h..j–1] <= x = b[j] <= b[j+1..k] # Make invariant true at start j = q = # inv: b[h..j–1] <= x = b[j] <= b[q+1..k] while : # post: b[h..j–1] <= x = b[j] <= b[j+1..k] <= x h j t k inv: b x ??? >= x

Partition Example # Make invariant true at start j = h t = k+1 # inv: b[h..j–1] <= x = b[j] <= b[t..k] while j < t–1: if b[j+1] <= b[j]: swap b[j] and b[j+1] j = j+1 else: swap b[j+1] and b[t-1] t=t–1 # post: b[h..j–1] <= x = b[j] <= b[j+1..k] # Make invariant true at start j = q = # inv: b[h..j–1] <= x = b[j] <= b[q+1..k] while : # post: b[h..j–1] <= x = b[j] <= b[j+1..k] <= x h j t k inv: b <= x h j q k inv: b x ??? >= x x ??? >= x

Partition Example # Make invariant true at start j = h t = k+1 # inv: b[h..j–1] <= x = b[j] <= b[t..k] while j < t–1: if b[j+1] <= b[j]: swap b[j] and b[j+1] j = j+1 else: swap b[j+1] and b[t-1] t=t–1 # post: b[h..j–1] <= x = b[j] <= b[j+1..k] # Make invariant true at start j = h q = k # inv: b[h..j–1] <= x = b[j] <= b[q+1..k] while j < q: # post: b[h..j–1] <= x = b[j] <= b[j+1..k] <= x h j t k inv: b <= x h j q k inv: b x ??? >= x x ??? >= x

Partition Example # Make invariant true at start j = h t = k+1 # inv: b[h..j–1] <= x = b[j] <= b[t..k] while j < t–1: if b[j+1] <= b[j]: swap b[j] and b[j+1] j = j+1 else: swap b[j+1] and b[t-1] t=t–1 # post: b[h..j–1] <= x = b[j] <= b[j+1..k] # Make invariant true at start j = h q = k # inv: b[h..j–1] <= x = b[j] <= b[q+1..k] while j < q: if b[j+1] <= b[j]: swap b[j] and b[j+1] j = j+1 else: swap b[j+1] and b[q] q=q–1 # post: b[h..j–1] <= x = b[j] <= b[j+1..k] <= x h j t k inv: b <= x h j q k inv: b x ??? >= x x ??? >= x

Partition Example # Make invariant true at start j = h t = k+1 # inv: b[h..j–1] <= x = b[j] <= b[t..k] while j < t–1: if b[j+1] <= b[j]: swap b[j] and b[j+1] j = j+1 else: swap b[j+1] and b[t-1] t=t–1 # post: b[h..j–1] <= x = b[j] <= b[j+1..k] # Make invariant true at start j = m = # inv: b[h..j–1] <= x = b[j] <= b[j+1..m] while : # post: b[h..j–1] <= x = b[j] <= b[j+1..k] <= x h j t k inv: b x ??? >= x

Partition Example # Make invariant true at start j = h t = k+1 # inv: b[h..j–1] <= x = b[j] <= b[t..k] while j < t–1: if b[j+1] <= b[j]: swap b[j] and b[j+1] j = j+1 else: swap b[j+1] and b[t-1] t=t–1 # post: b[h..j–1] <= x = b[j] <= b[j+1..k] # Make invariant true at start j = h m = h # inv: b[h..j–1] <= x = b[j] <= b[j+1..m] while : # post: b[h..j–1] <= x = b[j] <= b[j+1..k] <= x h j t k inv: b <= x h j m k inv: b x ??? >= x x >= x ???

Partition Example # Make invariant true at start j = h t = k+1 # inv: b[h..j–1] <= x = b[j] <= b[t..k] while j < t–1: if b[j+1] <= b[j]: swap b[j] and b[j+1] j = j+1 else: swap b[j+1] and b[t-1] t=t–1 # post: b[h..j–1] <= x = b[j] <= b[j+1..k] # Make invariant true at start j = h m = h # inv: b[h..j–1] <= x = b[j] <= b[j+1..m] while m < k: # post: b[h..j–1] <= x = b[j] <= b[j+1..k] <= x h j t k inv: b <= x h j m k inv: b x ??? >= x x >= x ???

Partition Example # Make invariant true at start j = h t = k+1 # inv: b[h..j–1] <= x = b[j] <= b[t..k] while j < t–1: if b[j+1] <= b[j]: swap b[j] and b[j+1] j = j+1 else: swap b[j+1] and b[t-1] t=t–1 # post: b[h..j–1] <= x = b[j] <= b[j+1..k] # Make invariant true at start j = h m = h # inv: b[h..j–1] <= x = b[j] <= b[j+1..m] while m < k: if b[m+1] <= b[j]: swap b[j] and b[m+1] swap b[j+1] and b[m+1] m = m+1; j=j+1 else: m = m+1 # post: b[h..j–1] <= x = b[j] <= b[j+1..k] <= x h j t k inv: b <= x h j m k inv: b x ??? >= x x >= x ???

Questions?