SUNY Geneseo Department of Computer Science


Designing Binary Search

{Date}

CSci 141, Spring 2004
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Hand out Lab 7

First hour exam next Tuesday, March 2

Questions?

Lab data analysis?

n Avg Time 2^n - 1 const of prop
10 567 1023 1.8042328042
15 900 32767 36.407777778
20 2000 1048575 524.2875
25 3500 33554431 9586.9802857
30 7000 1073741823 153391.689

Practice test, question 3

Induction and upcoming test?

Mini-Assignment

    int sumEvens( int n ) {
        if ( n == 2 ) {
            return 2;
        }
        else {
            return n + sumEvens( n - 2 );
        }
    }

Recurrence

Prove that f(n) = (3n-4)/2 is a closed form for this

Binary Search

End-to-end design, correctness, and performance analysis example.

Search a section of a sorted array between positions low and high for a value x. Return true if x is there, false if not.

[Search for x Between Positions low and high of A]

Look at element n/2, see if < x, > x, or = x

    boolean search( A, x, low, high )
        if ( low <= high )
            mid = (low + high) / 2
            if ( A[mid] < x )
                return search( A, x, mid+1, high )
            else if ( A[mid] > x )
                return search( A, x, low, mid-1 )
            else
                return true
        else
            return false

Try to prove that this works

Next


Next Lecture