View Single Post
  #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