Raised This Month: $ Target: $400
 0% 

Example code for sorting an array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
jtp10181
Veteran Member
Join Date: May 2004
Location: Madison, WI
Old 06-15-2004 , 21:04   Example code for sorting an array
Reply With Quote #1

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.
__________________
jtp10181 is offline
Send a message via ICQ to jtp10181 Send a message via AIM to jtp10181 Send a message via MSN to jtp10181 Send a message via Yahoo to jtp10181
devicenull
Veteran Member
Join Date: Mar 2004
Location: CT
Old 06-15-2004 , 21:19  
Reply With Quote #2

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 } } }
__________________
Various bits of semi-useful code in a bunch of languages: http://code.devicenull.org/
devicenull is offline
jtp10181
Veteran Member
Join Date: May 2004
Location: Madison, WI
Old 06-15-2004 , 21:28  
Reply With Quote #3

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.
__________________
jtp10181 is offline
Send a message via ICQ to jtp10181 Send a message via AIM to jtp10181 Send a message via MSN to jtp10181 Send a message via Yahoo to jtp10181
devicenull
Veteran Member
Join Date: Mar 2004
Location: CT
Old 06-15-2004 , 22:34  
Reply With Quote #4

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)
__________________
Various bits of semi-useful code in a bunch of languages: http://code.devicenull.org/
devicenull is offline
BAILOPAN
Join Date: Jan 2004
Old 06-15-2004 , 22:48  
Reply With Quote #5

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       }    } }
__________________
egg
BAILOPAN is offline
QwertyAccess
Veteran Member
Join Date: Feb 2004
Location: Enjiru Layer
Old 06-15-2004 , 22:50  
Reply With Quote #6

hehe bailopan thats a great reply,

BubbleSorts
Pros: Simplicity and ease of implementation.
Cons: Horribly inefficient.
__________________
QwertyAccess is offline
jtp10181
Veteran Member
Join Date: May 2004
Location: Madison, WI
Old 06-16-2004 , 02:21  
Reply With Quote #7

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.
__________________
jtp10181 is offline
Send a message via ICQ to jtp10181 Send a message via AIM to jtp10181 Send a message via MSN to jtp10181 Send a message via Yahoo to jtp10181
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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