CS241: Spring 2015 -- Lecture 18, table and radix

  1. Admistrivia
    1. Midterm Friday, 3/13
    2. Jury duty, 3/12 (!)
    3. My job/your job
  2. Write a method which is passed an int[] list, and two indices, firstI, and lastI; it should reverse all the values between firstI and lastI, inclusive. E.g. if the initial list were: 1, 2, 3, 4, 5, 6; firstI=2, lastI=4, the result should be: 1, 2, 5, 4, 3, 6
  3. Radix sort pseudocode
    	radixSort(List aList) {
                create an array of queues - Queue[] qs = new Queue[10];
                initialize each Queue (! better than 10 null ptrs!)
    
                for each digit, d (from smallest to largest significance, i.e. 1, then 10, etc) {
                    for each element, e of aList{
                        qs[value at d].enqueue(e);
                    }
    
                    clear aList
                    for each queue from 0-9 {
                        while (!queue[i].isEmpty()) {
                            aList.add(queue[i].dequeue());
                        } // while
                    } // for each q
                } // for each digit
    	} // radixSort
            
    A question: Is this easier to understand as:
    	radixSort(List aList) {
                create an array of queues - Queue[] qs = new Queue[10];
                initialize each Queue (! better than 10 null ptrs!)
    
                for each digit, d (from smallest to largest significance, i.e. 1, then 10, etc) {
                    listToQueues() 
                    clear aList
                    queuesToList() 
                } // for each digit
    	} // radixSort
    
            listToQueues() {
                for each element, e of aList{
                    qs[value at d].enqueue(e);
                }
            }
    
            queuesToList() {
                for each queue from 0-9 {
                    while (!queue[i].isEmpty()) {
                        aList.add(queue[i].dequeue());
                    } // while
                } // for each q
            }
            
  4. Questions about Quick sort pseudocode? While what?
    	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)
                }
    	}
            
  5. Questions about 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
                }
    	}
            
  6. Proof by induction
    1. But first, the natural numbers (N)!
      1. 1 ∈ N
      2. n ∈ N => n+1 ∈ N
    2. To prove some proposition P(n) for all n
      1. Establish the base case - P(1)
      2. Show inductive step - P(k)=>P(k+1)
  7. Table sort (also known as counting sort) pseudocode
    	tableSort(List aList) {
    		//count em
    		create an array of counters
    		for each element, e {
                        counter[e]++
    		}
    		// spew
    		clear aList
    		for each counter (counter[i]) {
                        emit that many i's (i.e. insert that many into aList)
    		}
    	}