Heap sort

Heap sort is implemented with the data structure heap.

Algorithm
  Build a heap with the sorting array, using recursive insertion.   Iterate to extract n times the maximum or minimum element in heap and heapify the heap.   The extracted elements form a monotonic subsequence.  Here is the code: Heapsort(A)

Build-heap(A)

for i = n to 1 do

swap(A[1],A[i])

n = n - 1

Heapify(A,1)

Build-heap(A)

n = |A|

For(i=floor|n/2| to 1)   do

Heapify(A,i)

Heapify(A,i)

left = 2i

right = 2i+1

if(left<=n)and (A[left]>A[i])then

max = left

else max = i

if(right<=n)and (A(right] > A[max]) then

max = right

if(max!=i)   then

swap(A[i],A[max])

Heapify(A,max) 