Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
[Tutorial] Sorting in java (Bubble Sort)
Author Message
Dan

Posted: 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.

 The Extension 'class' was deactivated by an board admin, therefore this Attachment is not displayed.

SortDouble.java
Description:
 Tutorial and code

Download
Filename:  SortDouble.java
Filesize:  3.16 KB
Downloaded:  2337 Time(s)

Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Sponsor
Sponsor

Aziz

Posted: Tue Jun 21, 2005 10:07 am   Post subject: (No 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
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 2 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: