Outline for June 1, 2009

Reading: §13.3 Guest Lecturer: Justin Cummins

  1. Recursive refresher: Fibonacci sequence
  2. Merge function
    1. Merges two sorted lists into a third sorted list
      1. Compare first item of each list
      2. Return the smaller value concatenated with the merge of the two lists (minus the smaller value)
      3. If one list is empty, the result is the other list (terminating case/condition)
    2. Linear time O(n): each compare places one item into the result, where the result has n items
  3. Mergesort function (see mergesort.py)
    1. One of the fastest general-purpose sorts
    2. Divide-and-conquer algorithm
    3. Parallelizes easily
    4. Python’s list sort is a modified mergesort
    5. Algorithm
      1. Divide list in half
      2. Recursively sort each half
      3. Merge the sorted parts together
      4. If a list has 0 or 1 element, it’s already sorted (terminating case/condition)
    6. Example: Mergesort class volunteers by height
    7. O(n log n) algorithm in worst case and average case
  4. Compare mergesort to selection sort on random list of integers
    1. Selection sort is a quadratic, O(n2), algorithm
    2. For 10 and 100 elements, selection sort slightly faster
    3. For 1000 elements, mergesort almost 10× faster
    4. For 10000 elements, mergesort over 100× faster