Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 148,885 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,219 people online right now. 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: 14,695

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.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month