Quicksort algorithm implementation - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Quicksort algorithm implementation (
/showthread.php?tid=638090)
Quicksort algorithm implementation -
arad55 - 26.07.2017
I'm trying to implement a quicksort algorithm in Pawn to sort a double-dimension array.
The second dimension of the array includes two variables: Some ID and score.
I will be sorting the array by the score variable (qs_arr[index][1]).
Now, everything works fine when the array size is 20, even when it's 100. But, if I raise it to 200, it won't work anymore (I guess infinite loop or something, because it won't print the result in the console as it should).
This is the code:
pawn Code:
stock Clan_Quicksort2(qs_arr[sizeof(cs)][sizeof(cs[])], qs_start = 0, qs_end = -1+sizeof(qs_arr), times = 0)
{
new i = qs_start, j = qs_end;
new pivot = (qs_start + qs_end)/2;
new pivotval = qs_arr[pivot][1];
while(i <= j)
{
while(qs_arr[i][1] < pivotval)
{
i++;
}
while(qs_arr[j][1] > pivotval)
{
j--;
}
if(i <= j)
{
new qs_tmp[sizeof(cs[])];
qs_tmp = qs_arr[i];
qs_arr[i] = qs_arr[j];
qs_arr[j] = qs_tmp;
i++;
j--;
}
}
if(qs_start < j)
{
qs_arr = Clan_Quicksort2(qs_arr, qs_start, j, times+1);
}
if(i < qs_end)
{
qs_arr = Clan_Quicksort2(qs_arr, i, qs_end, times+1);
}
return qs_arr;
}
Do you see any reason it wouldn't work on large arrays?
Re: Quicksort algorithm implementation -
OneDay - 26.07.2017
https://github.com/oscar-broman/md-sort
Re: Quicksort algorithm implementation -
arad55 - 26.07.2017
Quote:
Originally Posted by OneDay
|
I saw that already, but still trying to implement my own code.