ArticleslgStudy

computer science

Heapsort

Heapsort is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Heapsort rather than just read about it. In short: In computer science, heapsort is an efficient, comparison-based sorting algorithm that reorganizes an input array into a heap (a data structure where each node is greater than its children) and then repeatedly removes the largest node from that heap, placing it at the end of the array in a similar manner to Selection sort. Although somewhat slower in practice on most machines than a well-implemented quicksort, it ha…

Heapsort — main illustration
Heapsort — illustration

Key takeaways

  • Heapsort belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Heapsort to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Heapsort from memory before moving on to harder problems.

Reference excerpt

In computer science, heapsort is an efficient, comparison-based sorting algorithm that reorganizes an input array into a heap (a data structure where each node is greater than its children) and then repeatedly removes the largest node from that heap, placing it at the end of the array in a similar manner to Selection sort. Although somewhat slower in practice on most machines than a well-implemented quicksort, it has the advantages of very simple implementation and a more favorable worst-case O(n log n) runtime. Most real-world quicksort variants include an implementation of heapsort as a fallback should they detect that quicksort is becoming degenerate. Heapsort is an in-place algorithm, but it is not a stable sort. Heapsort was invented by J. W. J. Williams in 1964. The paper also introduced the binary heap as a useful data structure in its own right. In the same year, Robert W. Floyd published an improved version that could sort an array in-place, continuing his earlier research into the treesort algorithm.

Overview The heapsort algorithm can be divided into two phases: heap construction, and heap extraction.

The heap is an implicit data structure which takes no space beyond the array of objects to be sorted; the array is interpreted as a complete binary tree where each array element is a node and each node's parent and child links are defined by simple arithmetic on the array indexes. For a zero-based array, the root node is stored at index 0, and the nodes linked to node i are iLeftChild(i) = 2⋅i + 1 iRightChild(i) = 2⋅i + 2 iParent(i) = floor((i−1) / 2) where the floor function rounds down to the preceding integer. For a more detailed explanation, see Binary heap § Heap implementation. This binary tree is a max-heap when each node is greater than or equal to both of its children. Equivalently, each node is less than or equal to its parent. This rule, applied throughout the tree, results in the maximum node being located at the root of the tree. In the first phase, a heap is built out of the data (see Binary heap § Building a heap). In the second phase, the heap is converted to a sorted array by repeatedly removing the largest element from the heap (the root of the heap), and placing it at the start of the sorted array. The heap is updated after each removal to maintain the heap property. Once all objects have been removed from the heap, the result is a sorted array. Heapsort is normally performed in place. During the first phase, the array is divided into an unsorted prefix and a heap-ordered suffix (initially empty). Each step shrinks the prefix and expands the suffix. When the prefix is empty, this phase is complete. During the second phase, the array is divided into a heap-ordered prefix and a sorted suffix (initially empty). Each step shrinks the prefix and expands the suffix. When the prefix is empty, the array is sorted.

Algorithm The heapsort algorithm begins by rearranging the array into a binary max-heap. The algorithm then repeatedly swaps the root of the heap (the greatest element remaining in the heap) with its last element, which is then declared to be part of the sorted suffix. Then the heap, which was damaged by replacing the root, is repaired so that the greatest element is again at the root. This repeats until only one value remains in the heap. The steps are:

Call the heapify() function on the array. This builds a heap from an array in O(n) operations. Swap the first element of the array (the largest element in the heap) with the final element of the heap. Decrease the considered range of the heap by one. Call the siftDown() function on the array to move the new first element to its correct place in the heap. Go back to step (2) until the remaining array is a single element. The heapify() operation is run once, and is O(n) in performance. The siftDown() function is called n times and requires O(log n) work each time, due to its traversal starting from the root node. Therefore, the performance of this algorithm is O(n + n log n) = O(n log n). The heart of the algorithm is the siftDown() function. This constructs binary heaps out of smaller heaps, and may be thought of in two equivalent ways:

given two binary heaps, and a shared parent node which is not part of either heap, merge them into a single larger binary heap; or given a "damaged" binary heap, where the max-heap property (no child is greater than its parent) holds everywhere except possibly between the root node and its children, repair it to produce an undamaged heap. To establish the max-heap property at the root, up to three nodes must be compared (the root and its two children), and the greatest must be made the root. This is most easily done by finding the greatest child, then comparing that child to the root. There are three cases:

If there are no children (the two original heaps are empty), the heap property trivially holds, and no further action is required. If root is greater than or equal to the greatest child, the heap property holds and likewise, no further action is required. If the root is less than the greatest child, exchange the two nodes. The heap property now holds at the newly promoted node (it is greater than or equal to both of its children, and in fact greater than any descendant), but may be violated between the newly demoted ex-root and its new children. To correct this, repeat the siftDown() operation on the subtree rooted at the newly demoted ex-root. The number of iterations in any one siftdown() call is bounded by the height of the tree, which is ⌊log2 n⌋ = O(log n).

Pseudocode The following is a simple way to implement the algorithm in pseudocode. Arrays are zero-based and swap is used to exchange two elements of the array. Movement 'down' means from the root towards the leaves, or from lower indices to higher. Note that during the sort, the largest element is at the root of the heap at a[0], while at the end of the sort, the largest element is in a[end].

procedure heapsort(a, count) is input: an unordered array a of length count (Build the heap in array a so that largest value is at the root) heapify(a, count)

… excerpt ends here. Continue reading the full article.

Illustrations

Heapsort illustration
Heapsort: Difference in time complexity between the "siftDown" version and the "siftUp" version.
Difference in time complexity between the "siftDown" version and the "siftUp" version.
Heapsort: An example of heapsort using Williams' heap-construction algorithm.
An example of heapsort using Williams' heap-construction algorithm.

Worked examples

Example 1 — a first encounter with Heapsort

Start with the simplest possible case. Write down what Heapsort claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Heapsort before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Heapsort ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Heapsort

In research
Heapsort appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Heapsort in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Heapsort is common in secondary-school and first-year university syllabi. It links to neighbouring topics Comparison sorts, Heaps (data structures), so understanding it makes those chapters shorter.
In everyday life
Look for Heapsort outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Heapsort in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Heapsort means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Heapsort out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Heapsort in simple terms?

In computer science, heapsort is an efficient, comparison-based sorting algorithm that reorganizes an input array into a heap (a data structure where each node is greater than its children) and then repeatedly removes the largest node from that heap, placing it at the end of the array in a similar…

Why does Heapsort matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Heapsort?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Heapsort.

Tags

  • Comparison sorts
  • Heaps (data structures)

Keep exploring