function HeapSort()
{
for(%i = $x / 2 - 1; %i >= 0; %i--)
siftDown(%i, $x);
for(%i = $x - 1; %i >= 1; %i-- )
{
%temp = $a[0];
$a[0] = $a[%i];
$a[%i] = %temp;
siftDown(0, %i-1);
}
}
function siftDown(%root, %bottom)
{
%done = false;
while(%root * 2 <= %bottom && !%done)
{
if(%root * 2 == %bottom)
%maxChild = %root * 2;
else if($a[%root * 2] > $a[%root * 2 + 1])
%maxChild = %root * 2;
else
%maxChild = %root * 2 + 1;
if($a[%root] < $a[%maxChild])
{
%temp = $a[%root];
$a[%root] = $a[%maxChild];
$a[%maxChild] = %temp;
%root = %maxChild;
}
else
%done = true;
}
}
There's heapsort. Basically the fastest sorting method you can use.
Replace $a with whatever array you need to sort, and $x with the length of the array.
If that doesn't run fast enough for you, konk yourself in the head a couple of times.