Array Sorting help!
#1

Код:
#undef MAX_PLAYERS
#define MAX_PLAYERS 10
new a[MAX_PLAYERS] = { 90 , 67 , 99 , 11 , 9 , 21 , 58, 0, 47 , 8 };
new b[MAX_PLAYERS];
i want the output as

Quote:

Sorted: 0 -> ID: 7
Sorted: 8 -> ID: 9
Sorted: 9 -> ID: 4
Sorted: 11 -> ID: 3
Sorted: 21 -> ID: 5
Sorted: 47 -> ID: 8
Sorted: 58 -> ID: 6
Sorted: 67 -> ID: 1
Sorted: 90 -> ID: 2
Sorted: 99 -> ID: 0

i can just sort sort a[] (quicksort) so i get the sorted array but i wanna show which sorted values belongs to which playerid ...

understand?
Reply
#2

I don't really get what you mean, but I came up with something, thanks to Ryder for his quickSort function.

Code
pawn Код:
new a[MAX_PLAYERS] = { 90 , 67 , 99 , 11 , 9 , 21 , 58, 0, 47 , 8 };
new b[MAX_PLAYERS];

main()
{
    quickSort(a, 0, sizeof(a) - 1);
       
    new
        i = 0;
       
    while(i != sizeof(a))
    {
        printf("%d", a[i]);
        i++;
    }
}

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);
}
Result/Output
Код:
input(42) : warning 203: symbol is never used: "b"
0
8
9
11
21
47
58
67
90
99
Error: read EIO (EIO)
Reply
#3

even i can do that, u have sorted the array variable 'a' .. ok thats done.. i want to allocate the playerid for which a[x] belongs to which playerid...

Quote:

Sorted: 0 -> ID: 7
Sorted: 8 -> ID: 9
Sorted: 9 -> ID: 4
Sorted: 11 -> ID: 3
Sorted: 21 -> ID: 5
Sorted: 47 -> ID: 8
Sorted: 58 -> ID: 6
Sorted: 67 -> ID: 1
Sorted: 90 -> ID: 2
Sorted: 99 -> ID: 0

in above example, sorted a[1] = 8 which it belongs to the playerid '9' ... can u understand?
Reply
#4

Then you will need to use an enum like

pawn Код:
enum NUMDATA
{
    number,
    pid
}
Then quicksort by number or playerid and swap any other values which will require copy pasting that quicksort function into a hard coded function that directly references your multidimensional variable.
Reply
#5

Try
pawn Код:
for(new i = (sizeof my_array)-1; i > 0; i--)
{
    if(my_array[i] >= my_array[i-1]) continue;
    my_array[i] ^= (my_array[i-1] ^= (my_array[i] ^= my_array[i-1]));
    i = sizeof my_array;
}
Reply
#6

pawn Код:
for(new i = (sizeof a)-1; i > 0; i--)
{
    if(a[i] >= a[i-1]) continue;
    a[i] ^= (a[i-1] ^= ( a[i] ^= a[i-1] ) );
    i = sizeof a;
    printf("%i", i);
}
Result
Код:
input(18) : warning 203: symbol is never used: "b"
10
10
10
10
10
10
10
10
10
Error: read EIO (EIO)

The server stopped.
Reply
#7

I don't actually know, why
pawn Код:
my_array[i] ^= (my_array[i-1] ^= (my_array[i] ^= my_array[i-1]));
fails right now. If I do
pawn Код:
my_array[i] ^= my_array[i-1];
my_array[i-1] ^= my_array[i];
my_array[i] ^= my_array[i-1];
(which normally should be almost the same as the first code), it just works fine.
Reply
#8

Create a multidimensional array with playerid and value and use this

Sort multi-dimensional arrays (enums supported)
Reply
#9

SOLVED! Thanks Guys!!

Код:
    new array[10][2];
    for(new j=0;j<10;j++)
	{
		array[j][0] = random(99); // Skill %
		array[j][1] = j; // playerid
	}
    SortDeepArray(array, 0);
    
    printf("Sorted \"array\":");
    
    for (new i = 0; i < sizeof(array); i++) {
        printf("    array[%d] = {%d, %d}", i, array[i][0], array[i][1]);
    }
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)