Question: why we didnt multiply n-1 by constant time? since we multiply n-1 by log n? Let us count operations line by line. 1. Construct the
Let us count operations line by line. 1. Construct the heap in linear time. "In particular, the BuildHeap procedure actually runs in (n) time". 2. Execute the loop and perform a logarithmic time operation n-1 times. 3. Other operations take constant time. Hence, your running time is HEAPSORT(A) | BUILD-MAX-HEAP(A) (n) n+ (n 1) logn + O(1) 2 for i + length[A] downto 2 3 = n + n logn - logn + O(1) do exchange A[1] A[i] 4 heap-size[A] + heap-size[A] - 1 = (n log n). 5 MAX-HEAPIFY(A, 1) (Ign) n-1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
