Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 107,710 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,098 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Bubble Sort

The bubble sort provides a simple way to sort an array. Simple modifications can change it to sort decrementally (highest to lowest).

Submitted By: SPlutard
Actions:
Rating:
Views: 10,056

Language: Java

Last Modified: August 9, 2006
Instructions: Feel free to copy & paste the bubbleSort method into your app.

Snippet


  1. /* Snippet: Bubble Sort Method
  2. * Author: SPlutard
  3. *
  4. * Description: The Bubble Sort method works like this:
  5. *      In a series of n-1 iterations, the successive elements, list[index] and list[index + 1] of list are compared.
  6. *      If list[index] is greater than list[index + 1], then the elements of list[index] and list[index + 1] are swapped.
  7. *      This process is repeated through the list until no swaps are necessary.
  8. *      On average, for a list with length 'n', the Bubble Sort method takes:
  9. *          key comparisons:    (n*(n-1))/2             
  10. *          item assignments:   (n*(n-1))/4             
  11. */
  12.  
  13. public class TestBubbleSort {
  14.     public static void main(String[] args) {
  15.         int unsortedArray[] = {10, 97, 6, 23, 0, -45, 697, -1000, 1, 0}; //Random set of numbers for example.
  16.         int i;
  17.        
  18.         bubbleSort(unsortedArray, unsortedArray.length); //Pass the array to be sorted and its length.
  19.        
  20.         System.out.println("After sorting, the list elements are: "); //Just to show you it worked. :)
  21.        
  22.         for(i=0; i<unsortedArray.length; i++) {
  23.             System.out.print(unsortedArray[i] + " ");
  24.         }
  25.     }
  26.  
  27.     private static void bubbleSort(int[] unsortedArray, int length) {
  28.         int temp, counter, index;
  29.        
  30.         for(counter=0; counter<length-1; counter++) { //Loop once for each element in the array.
  31.             for(index=0; index<length-1-counter; index++) { //Once for each element, minus the counter.
  32.                 if(unsortedArray[index] > unsortedArray[index+1]) { //Test if need a swap or not.
  33.                     temp = unsortedArray[index]; //These three lines just swap the two elements:
  34.                     unsortedArray[index] = unsortedArray[index+1];
  35.                     unsortedArray[index+1] = temp;
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }

Copy & Paste


Comments


edukondalut 2008-07-07 01:14:32

its better to execute in run time too.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month