Tuesday, April 3, 2007

Lecture 25

Recursion

  • Stack
    • space for parameters / local values/ return values
  • indirect recursion
    • a method which calls iteself with different parameters
  • recursion
    • a method which calls itself within itself
      • if this is done wrong you get a stack overflow error

int fact(int n)
{
//base case

if (n == 1) { return n}
return n * fact(n-1);
}

  • famous recursive methods
    • binary search
    • mergesort
    • quick sort
    • towers of hanoi

int bSearch(int[]A, int key, int l, int r)

{
// base case

if (l>r) return -(l + 1)
int m = (l + r) / 2;
if (A[m] == key ) return m;
if (A[m] < face="courier new">return bSearch( A, key, m+1, r)
return bSearch(A, key, L, m-1);

}


these were the notes i got when turnpin was in class, he doesn't write on the board too often so i hope this helps.

No comments: