Question
4. What is the maximal number of swap operations performed during the process of selecting consecutive minima during the execution of the heap-sort algorithm? In
4. What is the maximal number of swap operations performed during the process of selecting consecutive minima during the execution of the heap-sort algorithm? In your count skip the swap operations performed during the process of creating the initial heap. Assume that the input array has size n. (i) Express your answer in the form of a sum. (ii) Remove the symbol of summation and analyze the order of growth. Hint: Make use of the formula: 1*21 +2*22 +3*23 ++N*2N =(N-1)*2N+1+2. procedure pushdown(first, last: integer); var r: integer; begin r := first; while r <= last div 2 do if last = 2*r then begin if A[r].key > A[2*r].key then swap(A[r],A[2*r]); r := last end else if A[r].key > A[2*r].key and A[2*r].key <= A[2*r+1].key then begin swap(A[r],A[2*r]); r := 2*r end else if A[r].key > A[2*r+1].key and A[2*r+1].key < A[2*r].key then begin swap(A[r],A[2*r+1]); r := 2*r+1 end else r := last end; { pushdown } procedure heapsort; var i: integer; begin for i := n div 2 downto 1 do pushdown(i,n); for i := n downto 2 do begin swap(A[1],A[i]); pushdown(1,i-1) end end; { heapsort }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started