Heapsort is a good example of algorithm design leveraging data structures. It is in fact just selection sort implemented with a heap data structure.

Selection Sort

Selection Sort is a basic sorting algorithm partitions the input array into sorted and unsorted regions. To find the smallest item, we performed a linear sweep through the unsorted portion of the array. The smallest item is then swapped with the th item in the array before moving on to the next iteration. Selection sort performs iterations, where the average iteration takes steps, for a total of time.

What if we improve the data structure? The two operations we use in selection sort are:

  • Find the smallest item,
  • Remove item from an unsorted array after it has been located,

These are exactly the operations supported by a priority queue! Thus, we can replace the array data structure with a better priority queue implementation, such as a heap or a balanced binary tree. The search operation now takes instead of , speeding up selection sort from to .