Code Snippets

  

Java Source Code


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

Join 137,226 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,033 people online right now. Registration is fast and FREE... Join Now!





Bubble Sort

Bubble sort with a few improvements made so that the best-case run time for this algorithm will be n.

Submitted By: Dark_Nexus
Actions:
Rating:
Views: 334

Language: Java

Last Modified: August 29, 2008
Instructions: This class has a Sort method which takes an array of Integer's. The class will then sort that array and return it.

Snippet


  1. class BubbleSort
  2. {
  3.      private Integer[] list;
  4.  
  5.      private void bubbleSort()
  6.      {
  7.           /*Sorts the list "sorted" by use of the bubble sort
  8.             algorithm with a few "improvements" made.  The first improvement
  9.             uses a boolean value to detect whether swaps were during a pass.
  10.             If no swaps were made, then the list is sorted and no furuther passes
  11.             are required.  The second optimization is the limit placed on the
  12.             range of the inner for loop.  After each pass, the the element at
  13.             index n - i has been placed properly, where n is the length of the list.
  14.             Thus, there is no need to look at the value again when making future
  15.             passes*/
  16.  
  17.           Integer tmp = null;          //Temporary variable for swapping
  18.           boolean hasSwapped = false;     //Boolean value to flag whether swaps were made
  19.           int len = list.length - 1;     //The pre-calculated index of the last element in the array
  20.  
  21.           for (int i = 0;i < len;i++)
  22.           {
  23.                hasSwapped = false; //Initialize hasSwapped to false for each pass
  24.  
  25.                for (int s = 0;s < len - i;s++)
  26.                {
  27.                     //If this element is greater than the next element
  28.                     if (list[s] > list[s + 1])
  29.                     {
  30.                          //Swap the elements
  31.                          tmp = list[s];
  32.                          list[s] = list[s + 1];
  33.                          list[s + 1] = tmp;
  34.                
  35.                          hasSwapped = true;     //Flag that a swap has been made
  36.                     }
  37.                }
  38.  
  39.                //If no swaps were made (sorting is complete)
  40.                if (hasSwapped == false)
  41.                     return;     //End the sort
  42.           }
  43.      }     
  44.  
  45.      public Integer[] sort(Integer[] unsorted)
  46.      {
  47.           /*Initializes the list to be sorted as the copy list passed
  48.              and then invokes a bubble sort algorithm on that list.  The
  49.             resulting sorted list will then be returned*/
  50.  
  51.           list = unsorted;
  52.  
  53.           bubbleSort();
  54.      
  55.           return list;
  56.      }
  57.      
  58.      BubbleSort()
  59.      {
  60.           /*Default constructor*/
  61.           list = null;
  62.      }
  63. }
  64.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

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

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month