Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-10-2007, 03:54 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Sorting Data: Bubble Sort

Simple bubble sort algorithm used when sorting data from least to greatest or from greatest to least. Bubble sorting is one of the slowest algorithms for sorting data, but, it's good enough when you have to sort arrays with a small number of data.


Least to Greatest:

PHP Code:
//#CLIENTSIDE
function onCreated()
{
  
this.example = {1,6,4,2,5,8,20,100,54,1000,523};
  
LeastToGreatest(this.example);
}

function 
LeastToGreatest(array) 
{
  for (
a=0a<array.size()-1a++) 
  {
    for (
b=0b<array.size()-1-ab++) 
    {
      if (array[
b+1] < array[b]) 
      {
         
this.temp = array[b];
         array[
b] = array[b+1];
         array[
b+1] = this.temp;
         
this.results = array;
         
player.chat this.results;
      }
    }
  }


Greatest to Least:

PHP Code:
//#CLIENTSIDE
function onCreated()
{
  
this.example = {1,6,4,2,5,8,20,100,54,1000,523};
  
GreatestToLeast(this.example);
}

function 
GreatestToLeast(array) 
{
  for (
a=0a<array.size()-1a++) 
  {
    for (
b=0b<array.size()-1-ab++) 
    {
      if (array[
b+1] > array[b]) 
      {
         
this.temp = array[b];
         array[
b] = array[b+1];
         array[
b+1] = this.temp;
         
this.results = array;
         
player.chat this.results;
      }
    }
  }


Last edited by Gambet; 01-10-2007 at 05:00 AM..
Reply With Quote
  #2  
Old 01-10-2007, 08:44 AM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
Good example and all... but sort of unnecessary.

I'm almost 100% sure that Graal has a built-in command for sorting arrays.
__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
  #3  
Old 01-10-2007, 05:21 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by Gambet View Post
Simple bubble sort algorithm used when sorting data from least to greatest or from greatest to least. Bubble sorting is one of the slowest algorithms for sorting data, but, it's good enough when you have to sort arrays with a small number of data.


Least to Greatest:

PHP Code:
... 

Greatest to Least:

PHP Code:
... 
Both examples are almost the same,
' if (array[b+1] < array[b]) ' is only changing

Nice script tho Gambet

Here is maybe how I would do it
PHP Code:
//#CLIENTSIDE
function onCreated()
{
  
temp.example = {1,6,4,25,8,20,100,54,1000,523};
  
doFunction"GreatestToLast"temp.example);
}

function 
doFunctionmode, array) 
{
  for ( 
temp.0temp.temp.array.size() - 1temp.++) 
  {
    for ( 
temp.0temp.temp.array.size() - temp.atemp.++) 
    {
      if ( 
temp.mode == "GreatestToLast")
      {
        if ( 
temp.array[ temp.1] > temp.array[ tempb]) 
        {
           
this.temp temp.array[ temp.b];
           
temp.array[ temp.b] = temp.array[ temp.1];
           
temp.array[ temp.1] = this.temp;
           
this.results temp.array;
           
player.chat this.results;
        }
      }else
      {
        if ( 
temp.mode == "LeastToGreatest")
        {
          if ( 
temp.array[ temp.1] < temp.array[ temp.b]) 
          {
             
this.temp temp.array[ temp.b];
             
temp.array[ temp.b] = temp.array[ temp.1];
             
temp.array[ temp.1] = this.temp;
             
this.results temp.array;
             
player.chat this.results;
          }
        }
      }
    }
  }

__________________
Reply With Quote
  #4  
Old 01-10-2007, 09:56 PM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Yes, would be more efficient for you to combine the two into one function. I separated the two merely just to show how to do each, and yes, to do one or the other only requires a flip in the symbol.


Quote:
Originally Posted by napo_p2p View Post
I'm almost 100% sure that Graal has a built-in command for sorting arrays.

???

You mean sortascending() and sortdescending() for TGraalVars?
Reply With Quote
  #5  
Old 01-11-2007, 12:21 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Chompy View Post
Both examples are almost the same,
' if (array[b+1] < array[b]) ' is only changing

Nice script tho Gambet

Here is maybe how I would do it
PHP Code:
blah blah blah 
PHP Code:
//#CLIENTSIDE
function onCreated(){
  
temp.example = {1,6,4,25,8,20,100,54,1000,523};
  
doFunction("GreatestToLast"temp.example);
}
function 
doFunction(mode, array) {
  
temp.= (mode=="GreatestToLast"1:(mode=="LeastToGreatest"? -1:0));
  
temp.len temp.array.size();
  for (
temp.0temp.temp.len 1temp.++) {
    for (
temp.0temp.temp.len temp.atemp.++) {
        if (
temp.s*temp.array[temp.1] > temp.s*temp.array[tempb]) {
           
this.temp temp.array[temp.b];
           
temp.array[temp.b] = temp.array[temp.1];
           
temp.array[temp.1] = this.temp;
           
this.results temp.array;
           
player.chat this.results;
        }
      }
    }
  }

Or something of the sort.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #6  
Old 01-11-2007, 03:01 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
Nice code
sortascending()/sortdescending() currently only sort strings, so it's not helping in all cases. You can also use sortbyvalue() but it is more made for sorting objects
Reply With Quote
  #7  
Old 01-11-2007, 04:19 PM
Draenin Draenin is offline
Magnificent Bastard
Draenin's Avatar
Join Date: Dec 2004
Location: Bermuda Triangle
Posts: 6,790
Draenin has much to be proud ofDraenin has much to be proud ofDraenin has much to be proud ofDraenin has much to be proud ofDraenin has much to be proud ofDraenin has much to be proud of
Send a message via AIM to Draenin Send a message via MSN to Draenin Send a message via Yahoo to Draenin
This is pretty useful. I have a tendency to do a lot of stuff the manual way too, because it makes it easier to insert or remove needed or unneeded actions in a function.
Reply With Quote
  #8  
Old 01-11-2007, 10:04 PM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Yes, figured this would be useful to people.

Using the built-in commands/functions is always a nice thing, but it does you no good if you don't understand the concepts behind them (i.e how to do them the manual way and so forth).

Quote:
Originally Posted by Stefan View Post
Nice code
sortascending()/sortdescending() currently only sort strings, so it's not helping in all cases. You can also use sortbyvalue() but it is more made for sorting objects

Which is why it's always nice to learn how to do things the manual way, just in case you're in a situation where you will need to script a function manually
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 01:50 AM.


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