38

I understand that both quick sort and merge sort need O(n) auxiliary space for the temporary sub-arrays that are constructed, and in-place quick sort requires O(log n) auxiliary space for the recursive stack frames. But for heap sort, it seems like it also has a worst case of O(n) auxiliary space to build the temporary heap, even if the nodes are just pointers to the actual elements.

I came across this explanation :

Only O(1) additional space is required because the heap is built inside the array to be sorted.

But I think this means the original array necessarily already had to be implemented as some sort of tree? If the original array was just a vector, it seems memory for a heap would still have to be allocated.

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
Herman Tran
  • 1,581
  • 2
  • 12
  • 19
  • 1
    it's not quick sort if it's not in-place. And you can merge in-place too. – Will Ness Mar 06 '14 at 23:10
  • 2
    It is sort-of possible to do a quicksort with constant auxiliary space, but it gets weird enough that many programmers question if it's still a quicksort. – Mooing Duck Oct 10 '14 at 20:02

4 Answers4

48

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
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
5

The cool trick here is since heap is a full binary tree, you can just use a plain array, and for item i, its parent would be the item i/2.

Judge Mental
  • 5,209
  • 17
  • 22
zw324
  • 26,764
  • 16
  • 85
  • 118
  • @MooingDuck I agree with you. Although one who knows the answer will figure out in a second. – HelloWorld123456789 Mar 06 '14 at 19:30
  • 5
    @RikayanBandyopadhyay: "Before you can understand recursion you must first understand recursion" – Mooing Duck Mar 06 '14 at 20:46
  • 1
    Thank you, this is also helpful – Herman Tran Mar 06 '14 at 22:00
  • 4
    This doesn't answer the question at all. – Jim Mischel Mar 06 '14 at 22:10
  • Nice. It is a cool property that allows us to build a heap inside the array itself to be sorted. I would add that heap can be thought of as a complete binary tree, where all levels except possibly the last are filled, and the nodes are as far to the left as possible. A full binary tree would indicate that the tree is completely filled on all levels. – mc9 Aug 26 '20 at 23:31
0
HEAP-SORT(A)
{
BUILD-MAX-HEAP(A)
if(i= A.length down to 2)
    exchange A[i] with A[1]
    A.heapSize = A.heapSize-1
    MAX-HEAPIFY(A,1)

}

i/p is stored in array which is passed to heap sort algorithm- HEAP-SORT(A). Array A is interpreted as tree and after BUILD-MAX-HEAP out of it and swapping last element with root and reducing size of heap each time by one and then call MAX-HEAPIFY(A,1) on it.

this all operations we are performing inside that array(A) only - which is given as i/p to algorithm. we are not using any extra space while performing this operation.. So space complexity - O(1).

Rudra
  • 1
0

Heap sort is an in-place algorithm; it doesn't require any extra space. Elements are rearranged during each recursive only inside the same array.

It provides a perception that a binary heap or tree is being formed, but in real scenario no tree or heap is being formed.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Parveen Kumar
  • 175
  • 3
  • 5