I just did some more research in the fast sorting algorithms. Seems you can increase the speed of a Quick Sort by using an Insertion Sort when you need to sort a really small collection (as in less than 20 objects). Many sources say you get the best performance by sorting collections smaller than 9 by using Insertion Sort, in actionscript I got the best results between 20 and 30.
So first we need an Insertion Sort:
private static function insertionSort(input:Array, left:int, right:int):void
{
var i:int, j:int, tmp:int, orgLeft:int = left - 1;
for (i = left + 1; i < right; i++)
{
tmp = input[i];
j = i - 1;
while (j >= 0 && tmp < input[j])
{
input[j + 1] = input[j];
j--;
}
input[j + 1] = tmp;
}
}
Now we can change our Quick Sort:
public static function quickSort2(arrayInput:Array, left:int, right:int):void
{
var i:int = left, j:int = right, pivotPoint:int = arrayInput[Math.round((left + right) * .5)], tempStore:int;
if (right - left < 25)
{
insertionSort(arrayInput, left, right);
}
else
{
while (i <= j)
{
while (arrayInput[i] < pivotPoint)
{
i++;
}
while (arrayInput[j] > pivotPoint)
{
j--;
}
if (i <= j)
{
tempStore = arrayInput[i];
arrayInput[i] = arrayInput[j];
i++;
arrayInput[j] = tempStore;
j--;
}
}
if (left < j)
{
quickSort2(arrayInput, left, j);
}
if (i < right)
{
quickSort2(arrayInput, i, right);
}
}
}