Quicksort algorithm implementation
#1

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?
Reply
#2

https://github.com/oscar-broman/md-sort
Reply
#3

Quote:
Originally Posted by OneDay
View Post
I saw that already, but still trying to implement my own code.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)