Lecture 13: Prelim Review

Topics possibly covered on the preliminary exam: Lectures 1–12.

Read the lecture notes!

Topics:

Basic SML competence

Programming methodology

Data structures

Asymptotic analysis

Structure of test:

Review

fun sort3(a: int list): int list =
  case a of
    nil => nil
  | [x] => [x]
  | [x,y] => [Int.min(x,y), Int.max(x,y)]
  | a => let
      val n = List.length(a)
      val m = (2*n+2) div 3
      val res1 = sort3(List.take(a, m))
      val res2 = sort3(List.drop(res1, n-m) @
                       List.drop(a, m))
      val res3 = sort3(List.take(res1, n-m) @
                       List.take(res2, 2*m-n))
    in
      res3 @ List.drop(res2,2*m-n)
    end

This algorithm actually does sort the list (I fixed it from the version given in class; the fix was to make sure the recursive sorts are applied to at least ceiling(2n/3) of the list.) But its run time is O(n2.71), which we can show from the recurrence:

T(n) = kn + 3T(2n/3)

By plugging in cn2 and cn3 for T(n), we find that cn2 grows more slowly than T(n) and cn3 grows more quickly. If we solve for the exponent that lets us construct both upper and lower bounds, we find that it is log3/23, or slightly less than 2.71!