Outline for May 29, 2009

Reading: §13.4


  1. Recursion review
    1. Express problem in terms of itself
    2. Recursive definitions
      1. Base case (not recursive)
      2. Recursive part (must eventually reduce to base case)
  2. Example: number of positive numbers in a list (see poslist.py)
    1. Definition
      1. Base case: empty list gives pos(lst) = 0
      2. Recursive part: if first element is positive, pos(lst) = 1 + pos(lst[1:]); otherwise, pos(lst) = pos(lst[1:])
    2. Show stack for lst = [ 3, -2, -1, 4 ]
  3. Example: greatest common denominator of m and n (see rgcd.py)
    1. Definition
      1. Base case: if n is 0, gcd(m, n) = m
      2. Recursive part: gcd(m, n) = gcd(n, m % n)
    2. Show stack for m = 12, n = 8
  4. Example: Tower of Hanoi (see hanoi.py)
    1. Definition
      1. Base case: 1 disk, just move it
      2. Recursive part: move n−1 disks to second pole, move bottom disk to third, move disks on second pole to third
    2. Show stack for 3 disks
  5. Sorting
    1. Why it is important
    2. A bit of history
  6. Selection sort (see selsort.py)
    1. Scan list to find smallest element
    2. Swap it and first element
    3. Repeat for rest of list