Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-03-2010, 04:51 AM
MrOmega MrOmega is offline
One More Time
MrOmega's Avatar
Join Date: Aug 2010
Location: TN, USA
Posts: 631
MrOmega is an unknown quantity at this point
Send a message via AIM to MrOmega Send a message via MSN to MrOmega Send a message via Yahoo to MrOmega
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"
__________________
Time is the fire in which we burn...
Up, Up, Down, Down, Left, Right, Left, Right, B, A, Select, Start! Now I got 99 LIVES!!!
Reply With Quote
  #2  
Old 11-03-2010, 04:58 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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);

__________________
Quote:
Reply With Quote
  #3  
Old 11-03-2010, 05:36 AM
salesman salesman is offline
Finger lickin' good.
salesman's Avatar
Join Date: Nov 2008
Location: Colorado
Posts: 1,865
salesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud of
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.
__________________
Reply With Quote
  #4  
Old 11-03-2010, 05:44 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
@Sales: http://forums.graalonline.com/forums...47&postcount=6

Also sortbyvalue is champ for typical Graal use.
__________________
Quote:
Reply With Quote
  #5  
Old 11-03-2010, 06:42 AM
12171217 12171217 is offline
Banned
Join Date: Jan 2009
Posts: 453
12171217 has a spectacular aura about
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?
Reply With Quote
  #6  
Old 11-03-2010, 02:50 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by 12171217 View Post
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
__________________
Quote:
Reply With Quote
  #7  
Old 11-03-2010, 04:38 PM
salesman salesman is offline
Finger lickin' good.
salesman's Avatar
Join Date: Nov 2008
Location: Colorado
Posts: 1,865
salesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud of
Quote:
Originally Posted by fowlplay4 View Post
@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.
__________________
Reply With Quote
  #8  
Old 11-03-2010, 07:06 PM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Quote:
Originally Posted by fowlplay4 View Post
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.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #9  
Old 11-03-2010, 09:03 PM
MrOmega MrOmega is offline
One More Time
MrOmega's Avatar
Join Date: Aug 2010
Location: TN, USA
Posts: 631
MrOmega is an unknown quantity at this point
Send a message via AIM to MrOmega Send a message via MSN to MrOmega Send a message via Yahoo to MrOmega
Quote:
Originally Posted by Tigairius View Post
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...
__________________
Time is the fire in which we burn...
Up, Up, Down, Down, Left, Right, Left, Right, B, A, Select, Start! Now I got 99 LIVES!!!
Reply With Quote
  #10  
Old 11-03-2010, 09:57 PM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Quote:
Originally Posted by MrOmega View Post
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.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #11  
Old 11-03-2010, 10:13 PM
MrOmega MrOmega is offline
One More Time
MrOmega's Avatar
Join Date: Aug 2010
Location: TN, USA
Posts: 631
MrOmega is an unknown quantity at this point
Send a message via AIM to MrOmega Send a message via MSN to MrOmega Send a message via Yahoo to MrOmega
Quote:
Originally Posted by Tigairius View Post
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
__________________
Time is the fire in which we burn...
Up, Up, Down, Down, Left, Right, Left, Right, B, A, Select, Start! Now I got 99 LIVES!!!
Reply With Quote
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 +2. The time now is 11:40 PM.


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