Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

Java Source Code


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

Join 300,314 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,057 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: 29,179

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.

sarvesh_tanwar 2009-01-08 11:01:21

i am making a project on byzentine intigrated secure aodv menet routing procotocol in java. plz help me in coding.thanks

dinesh_123 2009-03-11 15:03:41

iam making online graphics editing system using java platform. plz help me in coding thanx

dinesh_123 2009-03-11 15:04:37

plz provide me code to edit the image using java servlets

zanyrogue 2009-04-14 13:02:48

What package is bubbleSort in?

rahulietlko 2009-08-30 05:50:00

plz get me the code for implemeting the "bubble sort in java in which input of numbers which are to be sorted are to be taken from a file"......get me code its vry necessary


Add comment


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





Live Java Help!

Be Social

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

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month