Computer Science Canada

[Tutorial] Sorting in java (Bubble Sort)

Author:  Dan [ Thu Sep 04, 2003 10:30 pm ]
Post subject:  [Tutorial] Sorting in java (Bubble Sort)

I made this while studying for my java exam. The tutorial is the comments in the attached .java file. i thought doing it this way may be easer.

Veary basic, made to teach the conspte of Bubble sorting

P.S. yes i know there are lots of spelling mistakes.

Author:  Aziz [ Tue Jun 21, 2005 10:07 am ]
Post subject: 

I was messing around trying to learn something (Im noobies to the max at java) and changed it around to this:

code:

  /////////////////////////
 //How to sort an array//
/////////////////////////

/****By Hacker Dan*****/

//This tutorial requies a basick undersatnding of java,
//arrays and for loops.

//To start you will need to know how to swicth the values
//of two vars. this ushley requires and a temp var to hold
//the value of one of them while they are being swiched.
//Ex:
//
//int myVarOne = 10;
//int myVarTwo = 20;
//int temp;
//
//temp = myVarOne;
//myVarOne = myVarTwo;
//myVarTwo = temp;
//
//now the value of myVarOne is in myVarTwo and
//the value of myVarTwo is in myVarOne

//Note:
//The flowing exampe of bubble sorting can be used with
//any array of a var type that can be compared with an
//boolean oprator.
//
//Also this example list the array in order of bigest value
//to smallest value. to chage this you must chage the if
//stament in the seconded for loop.

//Edit by Aziz:
//Changed to sort strings. Just fooling around. All credit to da Dan :)

public class SortString
{
    public static void sort(String words[])              //Sorting fuction, the array of nums is passed to it
    {
        String temp;                                    //a temp var is need to swtich the values in the array
       
        for(int i = 0; i < words.length - 1; i++)        //Start of the bubble sort, you need to take one away from
        {                                               //the legnth of the array b/c you dont need to go throw the
            for(int j = 0; j < words.length - 1; j++) //array that many times. Also if it whould go passt the end of
            {                                           //the array in the second loop wich whould not been good.
                if(words[j].compareTo(words[j+1]) > 0)               //This part checks to see if the number before it is bigger
                                                                    //You must use the compareTo() method of a string
                {                                       //Note: chage the < opprator to > to sort from samllest to lagergets
                    temp = words[j];                    //Sotres the value of nums[ii] in the temp var for use latter
                    words[j] = words[j+1];              //puts the value of the bigger number where the lesser one was
                    words[j+1] = temp;                  //puts the value of the lesser var back in the array where the
                }                                       //biger one was.
            }
        }
    }
   
    public static void main(String[] args)
    {
         //declares an array to be sorted
         String words[] = {"Hacker Dan", "Tony", "Aziz", "dark_knight_27", "Coutsos", "krishon", "JayLo"};
         
         System.out.println("Before sort():\n");
         for(int i = 0; i < words.length; i++)      //for loop to show all the values of the array
         {
             System.out.println(words[i]);          //uses the for loop index var to slect an array entreay
         }
         
         sort(words);                               //sends the array to the sort() fuction
         
         System.out.println("\nAfter sort():\n");
         for(int i = 0; i < words.length; i++)      //for loop to show the realostes of the sorting
         {
             System.out.println(words[i]);
         }
    }
}

//By Hacker Dan
//copy right 2003 to compsci.ca and Hacker Dan


: