SUNY Geneseo Department of Computer Science


Divide and Conquer

Tuesday, February 5

CSci 242, Spring 2013
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture

Questions?

Divide and Conquer

Median value in an unsorted array A[1..n] (or generally kth smallest, median is n/2-smallest)

Tree of comparisons with n! leaves

        kthSmallest( A, first, last, k )
            if last < first
                // array section is empty, error!
            else
                p = partition( A, first, last )        // as in Quicksort
                if k < p
                    kthSmallest( A, first, p-1, k )
                else if k > p
                    kthSmallest( A, p+1, last, k+p )
                else
                    return A[p]

Sum of n/2^i approaches 2n

Hand out divide and conquer problem set

Next

How do you ensure that a divide-and-conquer algorithm is correct? Proof by induction!

No reading, but think about how you would prove the kth-smallest algorithm correct, and be ready to talk about doing it


Next Lecture