CS210 Tues, May 2

In pa06, we implement OrderedList with BSTwithRank.  Then get() is almost the same as findKth(), the only difference being that element positions start with 0 for get and with 1 for findKth.

OrderedList is supposed to allow duplicates, whereas BST’s disallow them by the fundamental rule.

Example: OrderedList has 2, 2, 4, 5, 5, 5, 10, 12, 12.  How can we represent this with a BST?

Answer: use count of duplicates for each key, put keys in BST.

            key=5
            dupct=3
            size=9
          /      \
     key=4        key=12
     dupct=1      dupct=2
     size=3       size=3
    /             /
key=2          key=10
dupct=2        dupct=1
size=2         size=1

Note that the size includes all duplicates at or below the node.
 
Example:   get(3) = findKth(4)
root:  leftSize = 3, 4 > 3, so not in left subtree
         leftSize + dupct = 3 + 3 = 6 >= 6, so 4 is at the node, with key=5.
   

Sorting, Chap. 8

We covering simple sorts, plus one serious sort, mergesort
Skipping an important method, quicksort, hopefully covered in cs310, and shell sort, less important

Simple sorts: too simple for good performance: T(N) = O(N2)
Better sorts, including merge sort: T(N) = O(NlogN)  much better for large N.

Simple sorts: insertion sort
Idea is to insert numbers into a sorted array, keeping it in order.
For each insert, have to move all the higher numbers down one spot in the array to make room for the new one.
That takes O(N), and we have to do this N times to get N numbers in order, so T(N) = O(N2).

Note that code in the book does insertion sort inside one array, using lower part for sorted numbers and higher part for as-yet unsorted numbers.

Merge Sort

pg. 313: the recursive approach--
  if array has 0 or 1 elements, return (it's already sorted)
  recursively sort upper and lower halves of array
  merge the two sorted halves

Picture of merge:  draw lines between numbers in middle here to see progression in merge:
First choose between first elements of two halves, then between 2 and 13 for second result, then between 13 and 15 for third, etc.

one sorted half:       |  1  |  13  |  24  |  26  |
                          1     3      5      6
                           2     4      7       8
other sorted half:     |  2  |  15  |  27  |  38  |

Clearly each choice is O(1), and there are O(N) choices, so the whole operation is O(N).

How to do it in code: see pictures in book showing progression of two "counters" down the arrays.  Each counter specifies where the merge has gotten to in the array.

HW: How to display numbers for problem 8.1
8.1(a): make table like Fig. 8.3
8.1(c)  Here's an example: sort 4  -1  3  10  12  4  6
(Halve means as close as possible.  Here I'm putting the extra one in the first sequence)
halve:             4  -1  3  10 |  12  4  6
halve again:    4  -1 |  3 10    12 |  4   6
and again:      4 | -1   3 | 10           4 | 6             (the 12 doesn't halve again)
returns:
merge            -1  4      3  10    12    4  6
merge:          -1  3  4  10           4  6  12
merge:           -1 3  4  4  6  10 12

In class, we also did a BST sort:  insert the numbers one by one into a BST, droppng the second 4 because it's a duplicate, then do removeMin one by one, showing  12 BST trees in all.