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) 