C# Code to implement Heap Sort

Kundnani.Rt
Posted by Kundnani.Rt under C# category on | Points: 40 | Views : 34019
I have implement for
int
array. How ever you can use any generic type using IComparable.

public static void HeapSort(int[] array, int arrayBound)
{
int i, j;

int leftChild, rightChild, Parent, root, temp;

root = (arrayBound - 1) / 2;

for (j = root; j >= 0; j--)
{

for (i = root; i >= 0; i--)
{

leftChild = (2 * i) + 1;

rightChild = (2 * i) + 2;

if ((leftChild <= arrayBound) && (rightChild <= arrayBound))
{

if (array[rightChild] >= array[leftChild])

Parent = rightChild;

else

Parent = leftChild;

}

else
{

if (rightChild > arrayBound)

Parent = leftChild;

else

Parent = rightChild;

}



if (array[i] < array[Parent])
{

temp = array[i];

array[i] = array[Parent];

array[Parent] = temp;

}

}

}

temp = array[0];

array[0] = array[arrayBound];

array[arrayBound] = temp;

return;

}

Comments or Responses

Login to post response