script

Friday, January 11, 2013

6.2-5


The code for MAX-HEAPIFY is quite efficient in terms of constant factors, except possibly for the recursive call in line 10, which might cause some compilers to produce inefficient code. Write an efficient MAX-HEAPIFY that uses an iterative control construct (a loop) instead of recursion. while(i<=A.length) l = left(i) r = right(i) if l <= A.length and A[l] > A[i] then largest = l else largest = i if r <= A.length and A[r] > A[largest] then largest = r; if largest != i then swap A[i] and A[largest] i = largest; else break

No comments:

Post a Comment