edited by
3,428 views
1 votes
1 votes

How many element comparisons would heap sort use to sort the integers $1$ to $8$ if they were

  1. initially in sorted order,  
  2. initially in reverse sorted order?
edited by

3 Answers

Best answer
4 votes
4 votes

Say u r talking about min heap

$I)$ For initially sorted order number of comparison will be $0$

$II)$ For reverse sorted order

for level $1$ (i.e. at root) number of comparison will be $0$

for level $2$ number of comparison for each element will be $1$

for level $3$ number of comparison for each element will be $2$

for level $4$ number of comparison for each element will be $3$

So, total number of comparison will be $0+1+1+2+2+2+2+3=13$

Now, to sort elements , we need to count first sortest element, 2nd shortest element like below logic 

https://gateoverflow.in/269595/madeeasy-subject-test-2019-programming-%26-ds-heap

Which takes $0+1+2+3+2+2+1=11$

So, total comparison will be $11+13=24$

//Similar thing happens for max heap too

edited by
0 votes
0 votes
in first case here nlgn time will take here  and in second case also nlgn comparisons will take here
0 votes
0 votes

check this link...

input 1 to 8 seperated by space...

for this number of comparisons is 27

 

input 8 to 1 seperated by space

for this number of comparisons is 24

https://www.google.com/url?sa=t&source=web&rct=j&url=http://www.allisons.org/ll/AlgDS/Sort/Heap/&ved=2ahUKEwjR3rCEuqjnAhX6xzgGHX-gC-kQFjABegQIDhAG&usg=AOvVaw1o1F4DS_PR2EydboEsaXDD&cshid=1580289366514

Related questions

0 votes
0 votes
0 answers
1
6 votes
6 votes
1 answer
2
thor asked Nov 23, 2016
4,177 views
Suppose we are sorting an array of eight integers using heapsort, and we have just finished some heapify (either maxheapify or minheapify) operations. The array now looks...
1 votes
1 votes
0 answers
3
Prajwal Bhat asked Dec 20, 2016
605 views
Need approach to solve this ?
1 votes
1 votes
1 answer
4
Rajesh Pradhan asked Nov 21, 2016
1,380 views
There are 3 D&C basis sorting AlgosQuick sort :- T(k)+T(n-k)+ CnMerge sort :- 2T(n/2) +Cn Heap sort :- ___________I know how the complexity of heap sort is O(nlogn) but ...