Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace MaxHeapSort
{
Class Program
{
Private static int [] myArray;
Private static int arraySize;
// Exchange data
Static void Exchange (int I, int j)
{
Int temp;
Temp = myArray [I];
MyArray [I] = myArray [j];
MyArray [j] = temp;
}
// Create a heap by traversing down nodes
Static void TraversingHeap (int vNode)
{
Int wNode = 2 * vNode + 1; // The Node wNode is the first subnode of the vNode.
While (wNode <arraySize)
{
// If the vNode has a second subnode, set wNode to the largest subnode under the vNode.
If (wNode + 1 <arraySize)
If (myarray [wnode + 1]> myarray [wnode])
Wnode ++;
// If the vnode is already the largest, return
If (myarray [vnode]> myarray [wnode])
Return;
// If the vnode is not, switch the vnode and wnode values.
Exchange (vnode, wnode );
// Continue searching for the next node
Vnode = wnode;
Wnode = 2 * wnode + 1;
}
}
// Create a heap
Static void buildheap ()
{
For (INT vnode = arraysize/2-1; vnode> = 0; vnode --)
Traversingheap (vnode );
}
Static void heapsort ()
{
Buildheap ();
While (arraysize> 1)
{
Arraysize --;
Exchange (0, arraysize );
Traversingheap (0 );
}
}
Static void Main (string [] args)
{
Int [] a = {42, 13, 24, 91, 23, 16, 05, 88 };
MyArray =;
ArraySize = a. Length;
HeapSort ();
For (int I = 0; I <a. Length; I ++)
Console. Write (a [I] + "");
Console. Read ();
}
}
}