23.02.2011, 11:49
Yet another version of quickSort implemented (from C++) to PAWN. I tested serveral times and it worked flawless (even a little bit faster than existing versions).
Example:
Output:
pawn Код:
stock quickSort(array[], left, right)
{
new
tempLeft = left,
tempRight = right,
pivot = array[(left + right) / 2],
tempVar
;
while(tempLeft <= tempRight)
{
while(array[tempLeft] < pivot) tempLeft++;
while(array[tempRight] > pivot) tempRight--;
if(tempLeft <= tempRight)
{
tempVar = array[tempLeft], array[tempLeft] = array[tempRight], array[tempRight] = tempVar;
tempLeft++, tempRight--;
}
}
if(left < tempRight) quickSort(array, left, tempRight);
if(tempLeft < right) quickSort(array, tempLeft, right);
}
pawn Код:
new
array[] = { -541, 54, 689, 12, 3, 0, 3, 55, 66, -541, 5468484, -564, 1554, 1656 }
;
quickSort(array, 0, sizeof(array) - 1);
for(new i; i != sizeof(array); ++i)
{
printf("%d", array[i]);
}
Код:
-564 -541 -541 0 3 3 12 54 55 66 689 1554 1656 5468484