6,870 views
9 9 votes
Why space complexity of heapsort is O(1)....and why not O(logn)..because of space required by recursion calls which is equivalent to height of the tree...where am i getting wrong plz help...

2 Answers

Best answer
9 9 votes

HEAP SORT uses MAX_HEAPIFY function which calls itself but it can be made using a simple while loop and thus making it an iterative function which inturn takes no space and hence Space Complexity of HEAP SORT can be reduced to O(1).

while ( i < = heapsize) {
 lc <- left(i)
 rc <- right(i)
 if (lc<=heapsize) and (A[lc]>A[i])
  largest <- lc
 else
  largest <- i 
 if (rc<=heapsize) and (A[rc]>A[largest])
  largest <- rc
 if (largest != i)
 {
   exchange A[i] <-> A[largest]
   i <- largest
 } 
 else
  break
}
selected by
0 0 votes
Position:
Show:

Related questions

0 0 votes
2 2 answers
5.4k
5.4k views
Hardik Maheshwari asked Jun 14, 2018
5,426 views
Since Heapify is a recursive function, its space complexity is $O(logn)$ because of the stack space required for recursion.I also read that space complexity of heapsort i...
1 1 vote
3 3 answers
3.5k
3.5k views
Akash Kumar Roy asked Apr 21, 2018
3,486 views
First read it properly. I am not asking a specific question about space complexity.Question: What is worst case space complexity of quick sort?Everywhere it is showing O(...
5 5 votes
1 1 answer
2.3k
2.3k views
Chhotu asked Nov 2, 2017
2,280 views
In general merge sort is not considered in-place sorting technique. Because an auxiliary array is used. If we will try to do it in-place in array data structure then our ...
1 1 vote
1 1 answer
1.2k
1.2k views
reena_kandari asked Jul 30, 2016
1,227 views
The number of elements that can be sorted in time using heap sort ?