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... Algorithms algorithms time-complexity space-complexity sorting binary-heap + – vineet.ildm 6.9k views answer comment Share Follow Print See 1 comment 1 1 comment reply Surajit commented Jan 3, 2017 reply Follow flag I think auxillary space required will be O(1) but no total space complexity,not sure. 0 0 replyShare Please log in or register to add a comment.
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 } Kamal Pratap answered Jan 28, 2017 • selected Nov 12, 2017 by vineet.ildm Kamal Pratap comment Share Follow See all 2 Comments 2 2 Comments reply moin commented Mar 7, 2017 reply Follow flag yes, i am agree with the kamal. since, recursive algorithm can be implemented using loops thats why we dont consider stack in space complexity of the algoritm thats why O(1) 0 0 replyShare Arnab Bhadra commented Jun 20, 2017 reply Follow flag Yes, We can implement HEAPIFY() recursive algorithm using loop, so no stack is required. Space complexity would be O(1). 0 0 replyShare Please log in or register to add a comment.
0 0 votes http://stackoverflow.com/questions/22233532/why-does-heap-sort-have-a-space-complexity-of-o1 Shubham Pandey 2 answered Nov 7, 2016 Shubham Pandey 2 comment Share Follow See all 2 Comments 2 2 Comments reply vineet.ildm commented Nov 7, 2016 reply Follow flag I have already seen this answer. But I am still not getting why space required by recursion calls is not considered. 0 0 replyShare sushmita commented Dec 5, 2016 reply Follow flag very nice question. i have the same doubt.. It should be log n because every time we are calling heapify on the root of tree? experts advice needed............... 1 1 replyShare Please log in or register to add a comment.