Sorting algorithms/Heapsort: Difference between revisions

Content added Content deleted
No edit summary
(end = end-1 must go before sift down, or else the algo doesn't work. See Wikipedia for the correct Psuedocode)
Line 13: Line 13:
''last element of the heap)''</span>
''last element of the heap)''</span>
swap(a[end], a[0])
swap(a[end], a[0])
<span style="color: grey">''(put the heap back in max-heap order)''</span>
siftDown(a, 0, end-1)
<span style="color: grey">''(decrement the size of the heap so that the previous''
<span style="color: grey">''(decrement the size of the heap so that the previous''
''max value will stay in its proper place)''</span>
''max value will stay in its proper place)''</span>
end := end - 1
end := end - 1
<span style="color: grey">''(put the heap back in max-heap order)''</span>
siftDown(a, 0, end-1)

'''function''' heapify(a,count) '''is'''
'''function''' heapify(a,count) '''is'''