Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Sorting Data: Bubble Sort (https://forums.graalonline.com/forums/showthread.php?t=71349)

Gambet 01-10-2007 03:54 AM

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;
      }
    }
  }



napo_p2p 01-10-2007 08:44 AM

Good example and all... but sort of unnecessary.

I'm almost 100% sure that Graal has a built-in command for sorting arrays.

Chompy 01-10-2007 05:21 PM

Quote:

Originally Posted by Gambet (Post 1263227)
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 :p
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;
          }
        }
      }
    }
  }



Gambet 01-10-2007 09:56 PM

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 (Post 1263301)
I'm almost 100% sure that Graal has a built-in command for sorting arrays.


???

You mean sortascending() and sortdescending() for TGraalVars?

Tolnaftate2004 01-11-2007 12:21 AM

Quote:

Originally Posted by Chompy (Post 1263352)
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 :p
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.

Admins 01-11-2007 03:01 PM

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

Draenin 01-11-2007 04:19 PM

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.

Gambet 01-11-2007 10:04 PM

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 (Post 1263687)
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 :)


All times are GMT +2. The time now is 01:51 AM.

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