CS241: Spring 2017 -- Theta, bubble sort, and Fibonacci

  1. Administriva
    1. Quiz Thurs!
    2. Tutors in lab TWTh ("...even then calendar, after Monday and Tues, says, 'WTF'!"
  2. The big picture: what's the plan?
    1. Acquire Java programming skill, expertise in common algorithms and data structures
      ...by implementing various algorithms using various data structures
    2. Learn to analyze the running times of those algorithms
      ...and verify, empirically, that the theoretical analysis are correct.
  3. A silly (ridiculously slow) sort: random sort for a list of n elements -- what is its running time? how to time it empirically??
        while the list is out of order (ooo){
                int i = rand(n); 
                int j = rand(n); 
                swap the ith and jth values in the list
        }
                
  4. What's wrong with this way to swap the ith and jth elements of an array?
        swap(list[i], list[j]);
    ...
        void swap(int x, int y) {
            int pocket = x;
            x = y;
            y = pocket;
        }
                
  5. Improved random sort; only swap if OOO! Is it faster? Is it's O running time different?
  6. A simple (not quite as slow) sort: Bubble sort for a list of n elements
        iterate n-1 times {
            for the ith element (from first to last-1) {
                if (out of order (relative to the next element) 
                    swap ith and ith+1th values, ith and next value?
            }  // each pair
        }  // outer loop
                
  7. How many operations will it take to bubble sort a list of n elements?
  8. What is the running time of an algorithm? How to talk about it explicitly.
    1. Theoretical: An intro to theta notation, by way of big-O notation.
      The running time of an algorithm, A, executed on input of length n, is denoted TAn.
      A is said to be O(f(n)) if there exist positive constants c, and n0 such that TAn < c * f(n), for all n > n0.
    2. Empirical: Time the algorithm!
    3. A difference between theory and practice: can something be trivial theoretically, yet impossible practically?
  9. You can trade space for time (and vice-versa). E.g. calculating Fibonacci numbers.
  10. If there is time: displaying Conway's Life graphically.
  11. If there is even *more* time (as if): reading files (and parsing them).
    1. A wrapper class for BufferedReader, MyReader
      1. String[] split()
      2. one character at a time
      3. StringTokenizer
      4. Scanner