DAA - Module 3 Heap sort
DAA - Module 3 Heap sort
Heaps
Heap is a partially ordered data structure that is especially suitable for implementing priority
queues. Priority queue is a multiset of items with an orderable characteristic called an item’s
priority, with the following operations:
Properties of Heap
1. There exists exactly one essentially complete binary tree with n nodes. Its height is
equal to ⌊𝑙𝑜𝑔2𝑛⌋
2. The root of a heap always contains its largest element.
3. A node of a heap considered with all its descendants is also a heap.
4. A heap can be implemented as an array by recording its elements in the top down, left-
to-right fashion. It is convenient to store the heap’s elements in positions 1 through n
of such an array, leaving H[0] either unused or putting there a sentinel whose value is
greater than every element in the heap. In such a representation,
a. the parental node keys will be in the first ⌊n/2⌋. positions of the array, while the
leaf keys will occupy the last ⌊n/2⌋ positions;
b. the children of a key in the array’s parental position i (1≤ i ≤⌊𝑛/2⌋) will be in
positions 2i and 2i + 1, and, correspondingly, the parent of a key in position i (2
≤ i≤ n) will be in position ⌊𝑛/2⌋.
Illustration
Bottom-up construction of a heap for the list 2, 9, 7, 6, 5, 8. The double headed arrows show
key comparisons verifying the parental dominance.
Analysis of efficiency - bottom up heap construction algorithm:
Assume, for simplicity, that n = 2k− 1 so that a heap’s tree is full, i.e., the largest possible
number of nodes occurs on each level. Let h be the height of the tree.
According to the first property of heaps in the list at the beginning of the section, h=⌊𝑙𝑜𝑔2𝑛⌋
or just ⌊𝑙𝑜𝑔2(𝑛 + 1)⌋= k − 1 for the specific values of n we are considering.
Each key on level I of the tree will travel to the leaf level h in the worst case of the heap
construction algorithm. Since moving to the next level down requires two comparisons—one
to find the larger child and the other to determine whether the exchange is required—the total
number of key comparisons involving a key on level I will be 2(h − i).
Therefore, the total number of key comparisons in the worst case will be
where the validity of the last equality can be proved either by using the closed-form formula
Delete an item from a heap: Deleting the root’s key from a heap can be done with the
following algorithm:
Maximum Key Deletion from a heap
1. Exchange the root’s key with the last key K of the heap.
2. Decrease the heap’s size by 1.
3. “Heapify” the smaller tree by sifting K down the tree exactly in the same way we did it
in the bottom-up heap construction algorithm. That is, verify the parental dominance
for K: if it holds, we are done; if not, swap K with the larger of its children and repeat
this operation until the parental dominance condition holds for K in its new position.
Illustration
Heap Sort
Heapsort - an interesting sorting algorithm is discovered byJ. W. J. Williams. This is a two-
stage algorithm that works as follows.
Stage 1 (heap construction): Construct a heap for a given array.
Stage 2 (maximum deletions): Apply the root-deletion operation n−1 times to the
remaining heap.
As a result, the array elements are eliminated in decreasing order. But since under the array
implementation of heaps an element being deleted is placed last, the resulting array will be
exactly the original array sorted in increasing order.
Heap sort is traced on a specific input is shown below:
Analysis of efficiency: Since we already know that the heap construction stage of the algorithm
is in O(n), we have to investigate just the time efficiency of the second stage. For the number
of key comparisons, C(n), needed for eliminating the root keys from the heaps of diminishing
sizes from n to 2, we get the following inequality:
This means that C(n) ∈ O(n log n) for the second stage of heapsort. For both stages, we get
O(n) + O(n log n) = O(n log n). A more detailed analysis shows that the time efficiency of
heapsort is, in fact, in Θ(n log n) in both the worst and average cases. Thus, heapsort’s time
efficiency falls in the same class as that of mergesort.
Heapsort is in-place, i.e., it does not require any extra storage. Timing experiments on random
files show that heapsort runs more slowly than quicksort but can be competitive with mergesort.
*****