Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   SortBubble( array, collation) (https://forums.graalonline.com/forums/showthread.php?t=134260992)

MrOmega 11-03-2010 04:51 AM

SortBubble( array, collation)
 
Well, redid this script and used the bubble method of sorting, simple to understand.

parameter:
1st - Array to Sort
2nd - Collation to use, if NULL it will use AlphaNumeric Collation

Script:
PHP Code:

function sortBubbletemp.list, temp.order)
{

  if ( !
temp.order)
  {
  
    
temp.order 
    {  
  
      
"`""~""!""@""#""$""%""^""&""*""("
      
")""-""_""=""+""[""{""]""}""|"";"
      
":"",""<""."">""/""?""0""1""2""3"
      
"4""5""6""7""8""9""A""a""B""b""C"
      
"c""D""d""E""e""F""f""G""g""H""h"
      
"I""i""J""j""K""k""L""l""M""m""N"
      
"n""O""o""P""p""Q""q""R""r""S""s"
      
"T""t""U""u""V""v""W""w""X""x""Y",
      
"y""Z""z"
    
    
};
  }
  
  for ( 
temp.0temp.< ( temp.list.size() ^ 2); temp.++)
  {

    if ( 
temp.swap)
    {
    
      
temp.=
      
temp.swap NULL;
      
    }

    if ( (
temp.1) == temp.list.size())
      break;

    
    while ( 
temp.obj1 == temp.obj2)
    {

      
temp.obj1 = ( @ temp.list[ temp.a]).substringtemp.start1);
      
temp.obj2 = ( @ temp.list[ temp.1]).substringtemp.start1);
      
temp.start ++;
      
      if (( @ 
temp.list[ temp.a]) == ( @ temp.list[ temp.1]))
        break;

    }

    if ( 
temp.order.indextemp.obj1) > temp.order.indextemp.obj2))
    {

      
temp.swap temp.list[ temp.a];
      
temp.list[ temp.a] = temp.list[ temp.1];
      
temp.list[ temp.1] = temp.swap;

    }
    
    
temp.obj1 
    
temp.obj2 
    
temp.start NULL;

  }

  return 
temp.list;


Usage:
PHP Code:

sortBubble( {"a""0""aa""a""How""Now""Brown""Cow""?""A""a"}) 

would give you this
PHP Code:

{"?","0","A","a","a","a","aa","Brown","Cow","How","Now"


fowlplay4 11-03-2010 04:58 AM

When I'm working with arrays and manipulating them I like to use obj.link(), basically removes the need to use a return.

PHP Code:

function onCreated() {
  
// Example Array
  
temp.arr "9 8 7 6 5 4 3 2 1".tokenize();
  
// Sort
  
bubbleSort(temp.arr.link());
  
// Array Sorted
  
echo(temp.arr);



salesman 11-03-2010 05:36 AM

It would be really cool if we had a built-in sort function that let us specify our own function for comparing objects (similar to c or java sorting methods).

example:
PHP Code:

temp.arr = {3412214421337};
temp.func = function(ab) { // should return <0 if (a < b), >0 if (a > b), or ==0 if (a == b)
  // do whatever you want to compare a and b
  // ex:
  
return b;
};

temp.arr.sort(..., temp.func); // would sort the array using my compare function 

I'm not sure if the above is syntactically correct...probably not, but you get the idea.

Actually, I might try to do something like this later when I'm not busy, but built-in would be better!

edit: bubble sort is a terrible sorting algorithm, and if you don't need to specify a specific order, you should stick to built-in sorting which probably uses quicksort or some other O(nlogn) sorting algorithm.

fowlplay4 11-03-2010 05:44 AM

@Sales: http://forums.graalonline.com/forums...47&postcount=6

Also sortbyvalue is champ for typical Graal use.

12171217 11-03-2010 06:42 AM

Wow.. If I had known obj.link() existed, so many of my scripts would be so much simpler.. Does it exist on both client and server?

fowlplay4 11-03-2010 02:50 PM

Quote:

Originally Posted by 12171217 (Post 1609903)
Wow.. If I had known obj.link() existed, so many of my scripts would be so much simpler.. Does it exist on both client and server?

yeah

salesman 11-03-2010 04:38 PM

Quote:

Originally Posted by fowlplay4 (Post 1609891)
@Sales: http://forums.graalonline.com/forums...47&postcount=6

Also sortbyvalue is champ for typical Graal use.

Awesome. That's what I was talking about.

Tigairius 11-03-2010 07:06 PM

Quote:

Originally Posted by fowlplay4 (Post 1609882)
When I'm working with arrays and manipulating them I like to use obj.link(), basically removes the need to use a return.

Technically more efficient to use link() too :)... Then again, we're talking about bubble sort here... So I don't think someone using this would care much about efficiency.

MrOmega 11-03-2010 09:03 PM

Quote:

Originally Posted by Tigairius (Post 1609963)
Technically more efficient to use link() too :)... Then again, we're talking about bubble sort here... So I don't think someone using this would care much about efficiency.

gee thanks...

Tigairius 11-03-2010 09:57 PM

Quote:

Originally Posted by MrOmega (Post 1609990)
gee thanks...

You shouldn't really be offended, I know you aren't the one who created the Bubble Sort algorithm. I'm only providing advice, you should be using a better sorting algorithm if you're worried about efficiency. Take a look at the algorithms for Merge, Heap, and Quick sort. Quicksort will work best if the data is not already sorted though.

MrOmega 11-03-2010 10:13 PM

Quote:

Originally Posted by Tigairius (Post 1610010)
You shouldn't really be offended, I know you aren't the one who created the Bubble Sort algorithm. I'm only providing advice, you should be using a better sorting algorithm if you're worried about efficiency. Take a look at the algorithms for Merge, Heap, and Quick sort. Quicksort will work best if the data is not already sorted though.

Wasn't offened xP It was sarcasm, which is oftenly lost on the internet


All times are GMT +2. The time now is 12:23 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.