31.12.2013 Views

Lecture 9: Binary Chop What is Binary Chop about?

Lecture 9: Binary Chop What is Binary Chop about?

Lecture 9: Binary Chop What is Binary Chop about?

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Course: Software Engineering<br />

Specification – first attempt<br />

// pre: Sorted(A)<br />

// post: A[result] = x<br />

int Search(int[] A, int x){<br />

…….<br />

}<br />

As usual, result denotes the value returned by the method.<br />

Course: Software Engineering<br />

First Problem – Suppose x <strong>is</strong> not in A?<br />

<strong>What</strong> answer would we like?<br />

» We could look for the boundary between the elements < x and<br />

those > x. Th<strong>is</strong> would allow, for instance, the method Search to be used for<br />

finding where<strong>about</strong>s in A a new element x could be inserted leaving A sorted.<br />

There are two ways of describing th<strong>is</strong> boundary using result<br />

Way 1: A[result] < x and A[result+1] > x<br />

Way 2: A[result–1] < x, and A[result] > x<br />

Look at the array boundaries:<br />

Way 1<br />

Way 2<br />

All elements of A are >x<br />

result = -1.<br />

result = 0<br />

All elements of A are < x<br />

result = (A.length) -1<br />

result = (A.length)<br />

Way 2 <strong>is</strong> our standard: result <strong>is</strong> the smallest index where the array<br />

element <strong>is</strong> > x (or A.length if all the elements are < x).<br />

<strong>Lecture</strong> 9: <strong>Binary</strong> <strong>Chop</strong> Slide Number 3<br />

<strong>Lecture</strong> 9: <strong>Binary</strong> <strong>Chop</strong> Slide Number 4<br />

We assume for the rest of th<strong>is</strong> lecture that the pre-condition Sorted(A) means implicitly that A <strong>is</strong><br />

not null and not empty.<br />

3<br />

Defining a specification of a method with parameters requires thinking <strong>about</strong> all the possible<br />

parameters’ values that can be passed to the method when it’s used in some other part of a program.<br />

So, in the previous slide we have initially thought of defining the result of the method Search to be<br />

the index of the element x in the array. A first problem that the method search could have in<br />

execution <strong>is</strong> when the given array does not include the variable x at all. The post-condition of the<br />

method doesn’t say anything <strong>about</strong> th<strong>is</strong> case. So if we were building the method search using the<br />

specification given in the previous slide our program would not necessarily provide a correct answer<br />

(if any at all!) We need to think then what we want our method to do for the particular case when the<br />

array A does not include x.<br />

A useful thing would be to provide the boundary within the array of where such element should have<br />

been included if it were there. Th<strong>is</strong> would for instance allow the method to be used in other methods<br />

that, for instance, need to include an element in a sorted array.<br />

In th<strong>is</strong> case, we can have two different definitions of post-conditions for Search, one which says that<br />

Search returns the index of an element which <strong>is</strong> smaller than x and such that the next element <strong>is</strong><br />

greater than x (way 1) or that Search returns the index of the first element found that <strong>is</strong> bigger than x<br />

and such that the previous one <strong>is</strong> an element smaller than x (way 2). How can now choose between<br />

these two different post-conditions? Again what we need to do <strong>is</strong> think <strong>about</strong> the possible extreme<br />

cases of values of the parameters. It can well be that the array includes all elements which are<br />

smaller than the given x, or that all its elements are bigger than x. In each of these two cases we need<br />

to see what the boundary values the variable result would assume. Looking at the table given above,<br />

it <strong>is</strong> clear that using (way 1) approach we would get into trouble when the array includes elements all<br />

smaller than x, because the result would be a negative index, Way 2 instead seems to give acceptable<br />

values for result in both extreme case.<br />

We therefore choose the second type of post-condition: The method will return the index of the<br />

smallest element included in A that <strong>is</strong> bigger than x, and in case all elements of A are smaller than x<br />

it will return the length of the array.<br />

4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!