Binary%20Search%20and%20Loop%20invariants - PowerPoint PPT Presentation

About This Presentation
Title:

Binary%20Search%20and%20Loop%20invariants

Description:

Title: CS211 Author: chew Last modified by: David Gries Document presentation format: On-screen Show (4:3) Other titles: Times New Roman ProN W3 ... – PowerPoint PPT presentation

Number of Views:138
Avg rating:3.0/5.0
Slides: 15
Provided by: Chew3
Category:

less

Transcript and Presenter's Notes

Title: Binary%20Search%20and%20Loop%20invariants


1
Binary Search and Loop invariants
  • Lecture 12A
  • CS2110 Spring 2014

2
Develop binary search in sorted array b for v
?

If v in b, h is index of rightmost occurrence of
v. If v not in b, h is index before where it
belongs.
3
Develop binary search in sorted array b for v
?
  • Better than Binary search in last lecture because
    it
  • Finds not a random occurrence of v but the
    rightmost one.Useful in some situations
  • If v is not in b, it gives useful information it
    belongs between bh and bh1
  • Works also when array is empty!

4
Develop binary search in sorted array b for v
?

Store a value in h to make this true
Get loop invariant by combining pre- and post-
conditions, adding variable t to mark the other
boundary
5
How does it start (what makes the invariant true)?
?

Make first and last partitions empty h
-1 t b.length
6
When does it end (when does invariant look like
postcondition)?
Stop when ? section is empty. That is when h
t-1. Therefore, continue as long as h ! t-1.
h -1 t b.length while ( )
h ! t-1
7
How does body make progress toward termination
(cut ? in half)and keep invariant true?
h -1 t b.length while ( h ! t-1 )
int e (ht)/2
8
How does body make progress toward termination
(cut ? in half)and keep invariant true?
h -1 t b.length while ( h ! t-1 )
int e (ht)/2
if (be lt v) h e
9
How does body make progress toward termination
(cut ? in half)and keep invariant true?
h -1 t b.length while ( h ! t-1 )
int e (ht)/2 if (be lt v) h e
else t e
10
Loop invariants
  • We used the concept of a loop invariant in
    developing algorithms to reverse a linked list
    and do a binary search on a sorted array.

11
Loop invariant Important part of every formal
system for proving loops correct.
  • Extremely useful tool in developing a loop.
    Create (first draft of) invariant from pre- and
    post-conditions, then develop the parts of the
    loop from precondition, postcondition, invariant.

12
Loop invariant Important part of every formal
system for proving loops correct.
  • Invariant can be written in English, mathematics,
    diagrams, or mixtures of these. The important
    points are precision, clarity.

inv b0..h lt v lt bt..b.length-1
inv b0..h lt v lt bt..
inv everything in b0..h is at most v,
everything in bt.. is greater than v
13
About notation bh..k. bh..k has k1h
elements
h h1 h2 h3

Convention The notation bh..k is used only
when h lt k1. For example, b0..-2 is not
allowed. When h k1, bh..k denotes the empty
segment starting at bh.
Use the formula 0!
14
Developing loop from pre, post, inv 4 loopy
questions
  • // pre
  • // inv
  • while ( b )
  • // inv b
  • // inv
  • // inv ! b
  • // post
Write a Comment
User Comments (0)
About PowerShow.com