For the complete documentation index, see llms.txt. This page is also available as Markdown.

6. Heapsort

Exercises

6.1-1

Available in the latest revision of the IM.

6.1-2

Available in the latest revision of the IM.

6.1-3

Available in the latest revision of the IM.

6.1-4

Available in the latest revision of the IM.

6.1-5

Available in the latest revision of the IM.

6.1-6

Available in the latest revision of the IM.

6.1-7

Available in the latest revision of the IM.

6.1-8

Available in the latest revision of the IM.

6.2-1

Instead of presenting the solution directly, I recommend a website for visualizing algorithms called VisuAlgo. Select the option to visualize a binary heap. Choose in the bottom left corner the menu item Create(A) - O(N) and enter the following content for AA=27,17,3,16,13,10,1,5,7,12,4,8,9,0. Press Go. You will see that nodes at indices 3 and 6 are swapped and later nodes at indices 6 and 13 are exchanged. In essence, this shows the effect of calling Max-Heapify(A,3).

6.2-2

Available in the latest revision of the IM.

6.2-3

The procedure Min-Heapify(A,i) differs from Max-Heapify(A,i) in the following ways:

  • The variable largest should be renamed to smallest.

  • At line 3, the second comparison operator should be reversed from > to <.

  • At line 6, the second comparison operator should be reversed from > to <.

  • Line 10 should be changed to call Min-Heapify(A,smallest).

The running times of both procedures are asymptotically the same.

6.2-4

Available in the latest revision of the IM.

6.2-5

Available in the latest revision of the IM.

6.2-6

Available in the latest revision of the IM.

6.2-7

Available in the latest revision of the IM.

6.3-1

Follow the instructions from Exercise 6.2-1 this time entering for AA=5,3,17,10,84,19,6,22,9 . Press Go and watch how the transformations are happening. You may also choose to monitor the process step by step.

6.3-2

For any positive real xx, it is by definition true that x1\lceil x \rceil \ge 1. What we need to prove here is that n/2h+11/2n/2^{h+1}\ge1/2 for 0hlgn0 \le h \le \lfloor \lg n\rfloor. We have

n2h+1n2lgn+1=n2n=1/2.\begin{align*} \frac{n}{2^{h+1}} &\ge \frac{n}{2^{\lg n+1}} \\ &= \frac{n}{2n} \\ &= 1/2. \end{align*}

6.3-3

Available in the latest revision of the IM.

6.3-4

Available in the latest revision of the IM.

The proof in the IM has several issues. The 4th "fact" is not true in general. It says

For nonnegative reals aa and bb, we have abab\lceil a \rceil b \ge \lceil ab \rceil.

A simple counterexample is a=1a=1 and b=1/2b=1/2. In the main proof bb is a positive integer and aa is a positive integer divided by two to the power of some positive integer.

Furthermore, "facts" 1 and 5 are not trivial, thus they must be formally proven.

Simpler inductive proof

We prove the statement by induction on hh. The base case h=0h=0 is true, since based on Exercise 6.1-8 an nn element heap has n/2\lceil n/2 \rceil leaves.

Consider an nn element heap. We want to find the number of nodes at height h>0h>0. Let’s remove all the leaves from the tree, so the number of remaining nodes is nn/2=n/2n - \lceil n/2 \rceil = \lfloor n/2 \rfloor. Crucially, because we stripped away the bottom layer, every node that was at height hh in the original heap is now exactly at height h1h-1 in the new heap. Therefore, the number of nodes at height hh in the original nn element heap is exactly equal to the number of nodes at height h1h-1 in a heap of size n/2\lfloor n/2 \rfloor. Therefore, we can apply the inductive hypothesis to bound the number of nodes at height hh. This gives

Number of nodesn/22(h1)+1n/22h=n2h+1.\text{Number of nodes} \le \left\lceil \frac{\lfloor n/2 \rfloor}{2^{(h-1)+1}} \right\rceil \le \left\lceil \frac{n/2}{2^h} \right\rceil = \left\lceil \frac{n}{2^{h+1}} \right\rceil.

This concludes the proof.

6.4-1

Available in the latest revision of the IM.

6.4-2

Available in the latest revision of the IM.

6.4-3

Even if the array is sorted in increasing order, after the first step, it will be turned into a max-heap. In both cases, assuming all elements of AA are distinct, the extraction phase dominates, since each extraction involves swapping the root with the last element and restoring the heap property. Therefore, the asymptotic running time will remain the same Θ(nlgn)\Theta(n \lg n), irrespectively whether the input array is sorted in increasing or decreasing order (see also Exercise 6.4-5). In both scenarios, we are consistently taking small elements from the bottom of the tree, putting them at the root, and forcing to sink back down O(lgn)O(\lg n) levels.

On the other hand, for special situations, like all numbers being equal, the best-case running time is Θ(n)\Theta(n). Here, the input is sorted in both directions. Since none of the children is greater than its parent all nodes demand Θ(1)\Theta(1) extraction time (no recursive calls will happen in Max-Heapify).

6.4-4

We build upon Exercise 6.2-7. Suppose we have a sufficiently large array AA of length nn with n/2\lceil n/2 \rceil zeros and n/2\lfloor n/2 \rfloor ones. In step 1, this array is turned into a max-heap of height h=lgnh=\lfloor \lg n \rfloor, with zeros as leaves. During the extraction phase, internal nodes will become roots before any leaf. Let us just focus on internal nodes at level h2h-2. Since h2=Ω(h)h-2=\Omega(h) and 2h2=Ω(n)2^{h-2}=\Omega(n), the total number of data movements (internal path length) is Ω(n)Ω(h)=Ω(nlgn)\Omega(n) \cdot \Omega(h)=\Omega(n\lg n). From another angle, data movements happen because 0s are sifting down whenever 1s are extracted at the root. For each of these first n/4n/4 extractions, the 0s placed at the root must sift down past the 1s to at least level h2h-2.

★ 6.4-5

See Theorem 1 in section 4 here.

Comments on the paper

The paper uses a slightly different nomenclature than the book. The number of elements is denoted by NN whilst nn denotes the number of levels, so the height is h=n1h=n-1.

Let us derive the average internal path length for the top 2n22^{n-2} elements, knowing that the root is one of them. At level n1n-1 we must have at least 2n32^{n-3} large keys to "pick up" those excluded at level nn. We have

i=1n2i2i1=12i=1n2i2i=12(2+(n12)2n1)=1+(n3)2n2>(n3)2n2\begin{align*} \sum_{i=1}^{n-2} i2^{i-1} &= \frac{1}{2}\sum_{i=1}^{n-2} i2^i \\ &= \frac{1}{2}(2+(n−1−2)2^{n−1}) \\ &= 1+(n−3)2^{n−2} \\ &> (n−3)2^{n−2} \end{align*}

Therefore, the average internal path length is greater than n3n-3. The summation (lines 1 -> 2) is explained here.

6.5-1

Follow the instructions from Exercise 6.2-1 this time entering for AA=15,13,9,5,12,8,7,4,0,6,2,1. Select the ExtractMax() menu item and press 1x (Once).

6.5-2

Available in the latest revision of the IM.

6.5-3

See Exercise 6.2-3 for Min-Heapify.

6.5-4

Here is the correct version.

The running time is O(lgn)O(\lg n) plus the overhead for mapping priority queue objects to array indices.

6.5-5

Line 5 is totally redundant. This error stems from needlessly overcomplicating the book with that satellite data story. In the 3rd edition, where only keys were stored, setting the newly allocated memory cell (usually filled with garbage) to -\infin was mandatory to avoid the potential "new key is smaller than current key” error.

6.5-6

Increasing the key of a node xx in a max-heap cannot violate the max-heap property between xx and its children, but can lead to the situation where the key of the parent of xx is smaller than the key of xx. Thus, we need to traverse the path upward in a tree and float the node xx up, so it lands in a correct position. This task is realized by the while loop in Max-Heap-Increase-Key. Replacing the loop by a call to Max-Heapify will not work, because it can only traverse a path downward in a tree.

6.5-7

Available in the latest revision of the IM.

6.5-8

Available in the latest revision of the IM.

6.5-9

Available in the latest revision of the IM.

6.5-10

Available in the latest revision of the IM.

6.5-11

Available in the latest revision of the IM.

Problems

6-1 Building a heap using insertion

Available in the latest revision of the IM.

6-2 Analysis of d-ary heaps

Available in the latest revision of the IM.

6-3 Young tableaus

Available in the latest revision of the IM.

Last updated