Why does heap sort have a space complexity of O(1)?

Data in an array can be rearranged into a heap, in place. The algorithm for this is actually surprisingly simple., but I won’t go into it here.

For a heap sort, you arrange the data so that it forms a heap in place, with the smallest element at the back (std::make_heap). Then you swap the last item in the array (smallest item in the heap), with the first item in the array (a largish number), and then shuffle that large element down the heap until it’s in a new proper position and the heap is again a new min heap, with the smallest remaining element in the last element of the array. (std::pop_heap)

data:         1 4 7 2 5 8 9 3 6 0

make_heap:   [8 7 9 3 4 5 6 2 1 0] <- this is a min-heap, smallest on right

pop_heap(1): [0 7 9 3 4 5 6 2 1 8] <- swap first and last elements
pop_heap(2): 0 [7 9 3 4 8 6 2 5 1] <- shuffle the 8 down the heap

pop_heap(1): 0 1 [9 3 4 8 6 2 5 7] <- swap first and last elements
pop_heap(2): 0 1 [9 7 4 8 6 3 5 2] <- shuffle the 7 down the heap

etc

So no data actually needs to be stored anywhere else, except maybe during the swap step.

For visualization, here’s that original heap shown in a standard form

make_heap 
           0
     2           1
  3     4     5     6
               8   7 9
pop_heap
           8                           1                           1
     2           1               2           8               2           5 
  3     4     5     6    ->   3     4     5     6    ->   3     4     8     6 
                   7 9                         7 9                         7 9

Leave a Comment