Heap Sort Lecture 35 Robb T. Koether Hampden-Sydney College Mon, Apr 25, 2016 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 1 / 14
1 Sorting 2 The Heap Sort Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 2 / 14
Outline 1 Sorting 2 The Heap Sort Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 3 / 14
Total Order Relations Definition (Total Order Relation) A total order relation on a set A is a relation that has the following properties. Reflexive: For all a A, a a. Anti-symmetric: For all a, b A, if a b and b a, then a = b. Transitive: For all a, b, c A, if a b and b c, then a c. Comparable: For all a, b A, a b, or b a. Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 4 / 14
Sorting The order is determined by an order operator: <, >, <=, or >=. These operators, together with == and!=, must define a total order on the class. Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 5 / 14
Inefficient Sorting Algorithms Most elementary sorting algorithms are inefficient for long lists. Examples Bubble Sort. Selection Sort. Insertion Sort. These algorithms have run times of order O(n 2 ). They are fine for short lists (length < 10, 000) and ok for length < 100, 000. Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 6 / 14
Efficient Sorting Algorithms The efficient sorting algorithms are more complicated. Examples Merge Sort Heap Sort Quick Sort These algorithms have run times of order O(n log n). They are fast for lists of any practical length. Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 7 / 14
Outline 1 Sorting 2 The Heap Sort Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 8 / 14
The Heap Sort Definition (Heap Sort) A heap is a binary tree that has the following structure: At each node, the value at that node is greater than or equal to each of its children. Every level except the lowest level is full. The nodes in the lowest level are as far to the left as possible. The Heap Sort algorithm uses a heap. The nodes are indexed from 0 to size - 1 in level order. Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 9 / 14
90 80 50 60 70 30 10 20 40 A heap Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 10 / 14
The Heap Sort algorithm is as follows. Heapify the tree (to be explained). Set end equal to size - 1 (last element). While end > 0, do the following. Swap the root value with the end value. Sift down the root value down through the tree (to be explained). Decrement end. Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 11 / 14
The siftdown() Function The siftdown() function proceeds as follows. Begin at the root node. While the node value is smaller than at least one of its active children (pos end), Swap it with the larger of its children. Make that child node the current node. Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 12 / 14
The heapify() Function The heapify() function proceeds as follows. Begin with node = end/2, i.e., the last node with a child. While node > 1, do the following Sift the node down. Decrement the node. Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 13 / 14
40 80 50 60 70 30 10 20 90 Swap 90 and 40 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
80 40 50 60 70 30 10 20 90 Swap 80 and 40 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
80 70 50 60 40 30 10 20 90 Swap 70 and 40 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
20 70 50 60 40 30 10 80 90 Swap 80 and 20 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
70 20 50 60 40 30 10 80 90 Swap 70 and 20 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
70 60 50 20 40 30 10 80 90 Swap 60 and 20 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
10 60 50 20 40 30 70 80 90 Swap 70 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
60 10 50 20 40 30 70 80 90 Swap 60 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
60 40 50 20 10 30 70 80 90 Swap 40 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
30 40 50 20 10 60 70 80 90 Swap 60 and 30 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
50 40 30 20 10 60 70 80 90 Swap 50 and 30 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
10 40 30 20 50 60 70 80 90 Swap 50 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
40 10 30 20 50 60 70 80 90 Swap 40 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
40 20 30 10 50 60 70 80 90 Swap 20 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
10 20 30 40 50 60 70 80 90 Swap 40 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
30 20 10 40 50 60 70 80 90 Swap 30 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
10 20 30 40 50 60 70 80 90 Swap 30 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
20 10 30 40 50 60 70 80 90 Swap 20 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
10 20 30 40 50 60 70 80 90 Swap 20 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
10 20 30 40 50 60 70 80 90 Done Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 14 / 14
90 80 50 60 70 30 10 20 40 0 1 2 3 4 5 6 7 8 9 Swap 90 and 40 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
40 80 50 60 70 30 10 20 90 0 1 2 3 4 5 6 7 8 9 Swap 80 and 40 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
80 40 50 60 70 30 10 20 90 0 1 2 3 4 5 6 7 8 9 Swap 70 and 40 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
80 70 50 60 40 30 10 20 90 0 1 2 3 4 5 6 7 8 9 Swap 80 and 20 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
20 70 50 60 40 30 10 80 90 0 1 2 3 4 5 6 7 8 9 Swap 70 and 20 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
70 20 50 60 40 30 10 80 90 0 1 2 3 4 5 6 7 8 9 Swap 60 and 20 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
70 60 50 20 40 30 10 80 90 0 1 2 3 4 5 6 7 8 9 Swap 70 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
10 60 50 20 40 30 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 60 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
60 10 50 20 40 30 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 40 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
60 40 50 20 10 30 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 60 and 30 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
30 40 50 20 10 60 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 50 and 30 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
50 40 30 20 10 60 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 50 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
10 40 30 20 50 60 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 40 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
40 10 30 20 50 60 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 20 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
40 20 30 10 50 60 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 40 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
10 20 30 40 50 60 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 30 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
30 20 10 40 50 60 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 30 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
10 20 30 40 50 60 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 20 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
20 10 30 40 50 60 70 80 90 0 1 2 3 4 5 6 7 8 9 Swap 20 and 10 Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14
10 20 30 40 50 60 70 80 90 0 1 2 3 4 5 6 7 8 9 Done Robb T. Koether (Hampden-Sydney College) Heap Sort Mon, Apr 25, 2016 15 / 14