CS241: Spring 2015 -- Lecture 15, n^2 sorts

  1. Dijkstra: "...programming is so inherently complex that, in order to manage it successfully, programmers need to harness every trick and abstraction possible. When expressing the abstract nature of computer science, he wrote The job [of operating or using a computer] was actually beyond the electronic technology of the day, and, as a result, the question of how to get and keep the physical equipment more or less in working condition became in the early days the all-overriding concern. As a result, the topic became --primarily in the USA-- prematurely known as 'computer science' --which, actually is like referring to surgery as 'knife science' -- and it was firmly implanted in people's minds that computing science is about machines and their peripheral equipment. Quod non [Latin: 'Which is not true']."
  2. Debugging
    1. general techniques
      1. Proactive (instead of reactive):
        1. Understand what you are doing before you commit to code
        2. test classes/methods by themselves!!
        3. Keep all your code simple
        4. Use good style: legible, flexible, robust
      2. Analysis
      3. Minimize/specialize the input -- test on the one small input that highlights the problem
      4. souts - diagnostic output
      5. the debugger
      6. write test code!
    2. infix to postfix with ()'s
  3. Meanwhile, back at the three n^2 sorts
    1. Bubble sort - n-1 passes, each compares each pair of adjacent elements and swaps if ooo
          iterate n-1 times {
              for the ith element (i ranging from 0 to n-1) {
                  if (out of order (relative to the next element) 
                      swap ith and i+1rst values, ith and next value?
              }
          }
                      
    2. Selection sort - n-1 passes; the ith finds the smallest remaining (i.e. at i or more) element and swaps it to position i
          iterate n-1 times (i ranging from 0 to n-1) {
              swap ith and indexOfSmallest from i to n-1
              }
          }
      In more detail:
          iterate n-1 times (for i=0 to n-1) {
              indexOfSmallest = findSmallestFromIthDown(i)
              swap ith and indexOfSmallest from i to n-1
              }
          }
                      
          int findSmallestFromIthDown(int i) {
              ...
          }            
                      
    3. Insertion sort - repeatedly insert one more element into a sorted list
          iterate n-1 times (i ranging from 1 to n-1) {
              for the ith element {
                  insert it where it goes in the sorted list above it (must shift down the list first)
              }
          }
      In more detail:
          iterate n-1 times (i=1 to n-1) {
              insertHere = findWhereItGoesAboveIth(i)
              pocket = list[i] // so it doesn't get shifted onto!
              shiftDown(insertHere, i)
              list[insertHere] = pocket
          }
    4. Cleaning up the examplesFromClass code.
  4. How many operations?
    1. bubble sort
    2. selection sort
    3. insertion sort
  5. You can trade space for time (and vice-versa). E.g. calculating Fibonacci numbers.