Proving Correctness of Heap Building Algorithm

  • Topic:
  • Thread starter evinda
  • Start date
  • Tags Tags
    Algorithm
1 reply · 2K views · last reply
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Nerd)

We are given the following algorithm:

Code:
1.BUILDHEAP(A) 
2.    for (i=floor(size(A))/2; i>=0; i--)
3.          HEAPIFY(A,i);

according to my notes, we could prove its correctness, proving the following sentence:

At the beginning of each iteration of the for loop at the lines 2-3, each node $i+1, i+2, \dots, n $ is the root of a max-heap.

Could you explain me why it suffices to prove the above sentence?

Also, how could we prove it? (Thinking)
 
You ignored "heapify". Assuming this is the standard "sift down" algorithm, I don't see that there's much of anything to prove. Sift down produces a heap, starting at the "root".