In computer science, heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved Selection sort: like selection sort, heapsort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element from it and inserting it into the sorted region. Unlike selection sort, heapsort does not waste time with a linear-time scan of the unsorted region; rather, heap sort maintains the unsorted region in a Heap Data structure to more quickly find the largest element in each step.

(“Heapsort” 2022)


  1. Create a heap which is ordered as you’d like to sort the list (e.g. Min heap property or Max heap property) from all elements in the list to be sorted
  2. Swap the first element in the list (the top of the heap) with the last element
  3. Limit the heap’s scope to not include the recently swapped element
  4. Heapify
  5. Goto step 2 unil the list is sorted


Heapsort implementation in python