In Class: Week 11, recursion

For Friday:

Copy over a new file into your cs21/class/week11/ subdirectory:

    $ cp ~newhall/public/cs21/week11/ .

For Wednesday:

Copy over a new file into your cs21/class/week11/ subdirectory:

    $ cp ~newhall/public/cs21/week11/ .
Or, if you did not do this on Monday, create a week11 subdirectory in your cs21/class directory and copy over my starting point files:
    $ cd 
    $ cd cs21/class
    $ pwd

    $ mkdir week11        
    $ cd week11
    $ pwd

    $ cp ~newhall/public/cs21/week11/* .

  1. Open We are going to write a recursive version of binarySearch, recursiveBinarySearch. To come up with the idea os the recursive algorithm, we want to first think of a recursive definition of binary search:

    The result of binarySearch on a list is:

    When we implement a recursive function, just like when we implement any function, we need to think about what needs to be passed to it (how many parameters and what type) and if it returns a value or not, and if so what type. The return value is easy, since we want the same return value as the iterative version of Binary Search (the index of the bucket that contains the matching value or -1 if no matching value). However, the recursive binary search may need different values passed into it to do the right thing.

    We then want to think about what are the recursive step(s) and what are the base case(s) where we will stop the recursive search.

  2. We can also think about sorting in a recursive way. We are going to think about a new sorting algorithm that uses the Divide and Conquer idea of binary search, but applied to sorting, and we will think about it in a recursive way.