CS241: Spring 2017 -- Recursion, merge sort, quick sort

  1. Recursion
    1. base case -- when the recursion stops (always start by writing this!)
    2. recursion -- where you suspend your disbelief
    3. fib(n)
    4. Implementing a queue using a single stack
      1. initialization is just instantiate the stack (it, of course, starts out empty!)
      2. isEmpty is trivial
      3. either enqueue or dequeue is simple -- the other more complicated
      4. say dequeue is return pop(); -- then we must arrange that each enqueue puts the new element at the bottom of the stack
      5. example: enqueue(1); enqueue(2); enqueue(3); then while (!isEmpty()) soutv dequeue(), should print 1, 2, 3
  2. Quick sort pseudocode
    	quickSort(List aList) {
    		if (size>1) {
    			select a pivot and split aList into head and tail (where everything in head is < pivot...) with the pivot in between
                            quickSort(head)
                            quickSort(tail)
    		}
    	}
            
  3. quicksorting 3, 2, 1, 6, 7, 8, 5, 4 (in place)
  4. Merge sort pseudocode
    	mergeSort(List aList) {
    		if (size>1) {
    			split aList roughly in half creating head and tail
                            mergeSort(head)
                            mergeSort(tail)
    			merge head and tail
    		}
    	}
            
  5. mergesorting 3, 2, 1, 6, 7, 8, 5, 4 (in place)
  6. From pseudocode to code; details! It's all about the details!? No. First it's about the representation; after you choose that, it's about the details.
    1. starting -- also known as initializing
    2. stopping
    3. in between
  7. Proactive debugging
    1. souts on enter and exit to recursive routines; like this:
          void foo(int[] list, int startI, int endI) {
              System.out.print("Top of foo: ");
              System.out.print(" startI = " + startI);
              System.out.print(" endI = " + endI);
              System.out.println(list.toString(startI, endI));
              
              ...foo code...
              
              System.out.print("bottom of foo: ");
              System.out.print(" startI = " + startI);
              System.out.print(" endI = " + endI);
              System.out.println(list.toString(startI, endI));       
          }
                      
    2. list:toString(firstI, lastI)