AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Example code for sorting an array (https://forums.alliedmods.net/showthread.php?t=2756)

jtp10181 06-15-2004 21:04

Example code for sorting an array
 
Code:
sort_maps() {     new x,y,z,d     new bool:swap,bool:done,bool:dupe     new temp[32]     for ( x = 0; x < totalmaps; x++ ) {         for ( y = x + 1; y < totalmaps; y++ ) {             swap = false             done = false             dupe = true                     for (z = 0; z < 32; z++) {                 if ( T_LMaps[x][z] != T_LMaps[y][z]) {                     if ( T_LMaps[x][z] > T_LMaps[y][z] && !done) swap = true                     done = true                     dupe = false                 }             }             if (swap) {                 temp = T_LMaps[x]                 T_LMaps[x] = T_LMaps[y]                 T_LMaps[y] = temp             }             else if (dupe) {                 for ( d = y; d < totalmaps; d++ ) {                     T_LMaps[d] = T_LMaps[d + 1]                 }                 y = totalmaps--                 x--             }         }     } }

Just wanted to share since I spent some time on this, dunno if anyone has done thins B4 in one of thier plugins or not.

The array being sorted is T_LMaps
There is also dupe checking in there and it could easily be taken out.

Please let me know if there is any more effecient way of doing this, this was the best I could come up with and it works at least.

devicenull 06-15-2004 21:19

Heres shorter code, not necessairly more efficent
Code:
new i,j,tmp for (i=0,i<=TOTALITEMS,i++) { for (j=i,j<=TOTALITEMS,j++) { if (array[i] > array[j]) { tmp = array[i] array[i] = array[j] array[j] = tmp } } }

jtp10181 06-15-2004 21:28

devicenull, that looks like it would work for numbers. Tried it on my array and it wont compile. errors about the array must be indexed.

My example is for a multidimentional array or strings. I forgot to mention that in my post.

devicenull 06-15-2004 22:34

I don't think it will work for how amx handles strings.. it'll work for numbers, and for strings with some modifications (Hint replace the 3 = with strcopy) :)

BAILOPAN 06-15-2004 22:48

Not to dis what you've done, as it is nice :] but you DID ask if there is a more efficient way ;]

What you both did is BubbleSort, the most inefficient way.

The best sorting algorithms are recursive, however recursion can cripple AMX's stack space very easily, so here is HeapSort.

Code. Note I am not sure if I'm passing 2D arrays right, it changed between 2.1 and 2.5. You may need to supply [24][] or [][SIZE] or [24][SIZE]
Code:
#define SIZE 32 new Strings[24][SIZE] //Quick reimplentation of strcmp().  is this necessary? stock fstrcmp(str1[], str2[]) {    new i = 0    for (i=0; i<SIZE; i++)    {       if (str1[i] != str2[i])       {          if (str1[i] > str2[i])             return 1          else             return -1       }    }    return 0 } HeapSort(Strings[][], xSize) {    new i, temp[SIZE]    new aSize = (xSize/2)-1    for (i = aSize; i >= 0; i--)    {       SiftDown(Strings, i, xSize)    }    for (i=xSize-1; i>=1; i--)    {       copy(temp, SIZE, Strings[0])       copy(Strings[0], SIZE, Strings[i])       copy(Strings[i], SIZE, temp)       SiftDown(Strings, 0, i-1)    } } SiftDown(Strings[][], root, bottom) {    new done, child, temp[SIZE]        done = 0    while ((root *2 <= bottom) && (!done))    {       if (root*2 == bottom) {          child = root * 2       } elseif (fstrcmp(Strings[root * 2], Strings[root * 2 + 1]) > 0) {          child = root * 2       } else {          child = root * 2 + 1       }       if (fstrcmp(Strings[root], Strings[child]) < 0)       {          copy(temp, SIZE, Strings[root])          copy(Strings[root], SIZE, Strings[child])          copy(Strings[child], SIZE, temp)          root = child       } else {          done = 1       }    } }

QwertyAccess 06-15-2004 22:50

hehe bailopan thats a great reply, http://www.nsarmslab.com/qwerty/smileys/inlove.gif

BubbleSorts
Pros: Simplicity and ease of implementation.
Cons: Horribly inefficient.

jtp10181 06-16-2004 02:21

Quote:

Originally Posted by devicenull
I don't think it will work for how amx handles strings.. it'll work for numbers, and for strings with some modifications (Hint replace the 3 = with strcopy) :)

I've been using it.... works fine for strings


Gonna look at BAILOPANs code later see if I can impliment it somehow.
----
Hmm what bailopan posted makes no sense to me at all :)
Prob works but I'm clueless, guess I will stick to what I got now unless someone wants to explain it a little more or point me someplace where I can read about it.


All times are GMT -4. The time now is 14:55.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.