Heapsort

A sorted array is a useful static data structure. You can use binary search to find things. If you have many many find(x) operations and very few add(x) or remove(x) operations, sorted arrays can be your best SSet implementation. ...especially if the add(x) and remove(x) operations are bunched together.

Example: Netflix searches it's list of movie titles millions of times a day, in serving client requests. They add movies to their collection when they make new deals with movie producers. Sometimes these deals cover 10s of movies at once. They remove movies from their collection as contracts with producers expire. This also may be 10s of movies at once.

Suppose Neflix has n = 5000 active movie titles adds m = 100 movies this month. Algorithm: (1) Sort the m titles. Merge the new list into the sorted array of n titles, creating a sorted array of size n+m. This can be done in O(m log(m) + n) time.

There are many methods to sort an array. We will study 4 of them (and build a 5th -- a combination).


HeapSort: You can sort with heap operations.

HSort(array A) {
	BinaryHeap H;
	for (int i = 0; i < A.size(); ++i) 
		H.add(A[i]);                     // cost O(log(n)), done n times.
	for (int i = 0; i < H.size(); ++i) 
		A[i] = H.remove(); // extractMin // cost O(log(n)), done n times.
}

Heapsort is described in ODS Chapter 11.1.3. It is implemented in udods/BinaryHeap.h.

  1. Build the heap from unsorted array in O(n) time.
    [ See constructor BinaryHeap:: BinaryHeap(array b) ]
  2. Convert from heap ordered to sorted in O(n*lg(n)) time.
    [ See BinaryHeap:: sort(array b) ]

Why constructor BinaryHeap:: BinaryHeap(array b) is faster than doing n add(x) operations.

                        ____________________ 62 _________                        
                       /                                 \
             _______ 60 ________                           61
            /                   \                        /    \
        _ 56 __                 _ 57 __                58       59
       /       \               /       \              /  \     /  \
    48          49          50          51                ...      55     
   /  \        /  \        /  \        /  \ /  \                     \
 32    33    34    35    36    37    38    39             ...         47
/  \  /  \  /  \  /  \  / \   / \   / \   / \                           \
0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15            ...            31

work/2 (number of items at level times number of trickle down steps)

1x5                            ____________________ 62 _________                        
                              /                                 \
2x4                 _______ 60 ________                          61
                   /                   \                        /    \
4x3            _ 56 __                 _ 57 __                58       59
              /       \               /       \              /  \     /  \
8x2        48          49          50          51                ...      55     
          /  \        /  \        /  \        /  \ /  \                     \
16      32    33    34    35    36    37    38    39             ...         47
       /  \  /  \  /  \  /  \  / \   / \   / \   / \                           \
0      0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15            ...            31
----
57 < n (work/2 --> n as n --> ∞