Code Snippets

  

Java Source Code


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

Join 131,923 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,739 people online right now. Registration is fast and FREE... Join Now!





Cocktail Sort

A Java implementation of the cocktail sorting algorithm

Submitted By: Dark_Nexus
Actions:
Rating:
Views: 391

Language: Java

Last Modified: August 29, 2008
Instructions: This class has a sort method which will take an array of Integer's and then pass that array through the cocktail sorting algorithm. The resulting sorted array will then be returned.

Snippet


  1. class CocktailSort
  2. {
  3.         /**
  4.         * Performs a bi-directional bubble sort on the passed set, also know as a cocktail sort.
  5.         * After making passes from left to right, the algorithm then passes from right to left
  6.         */
  7.         private Integer[] cocktailSort(Integer[] unsorted) {
  8.                 int beg = 0;                           //Beginning index for the left-to-right pass
  9.                 int end = unsorted.length - 1;        //End index for the left-to-right pass
  10.                 Integer tmp = null;                  //Temporary variable for swapping
  11.                 boolean hasSwapped;                //Denotes whether a swap was made during a pass
  12.  
  13.                 do {
  14.                         hasSwapped = false;
  15.  
  16.                         //Bubble sort from left-to-right starting at beg and ending at end
  17.                         for (int i = beg;i < end;++i) {
  18.                                 if (unsorted[i] > unsorted[i + 1]) {
  19.                                 tmp = unsorted[i];
  20.                                 unsorted[i] = unsorted[i + 1];
  21.                                 unsorted[i + 1] = tmp;
  22.  
  23.                                  hasSwapped = true;
  24.                                     }
  25.                                }         
  26.                                end--;     //Decrease end by one
  27.  
  28.                                //Bubble sort from right-to-left starting at end and ending at beg
  29.                                for (int i = end;i > beg;--i) {
  30.                                        if (unsorted[i] < unsorted[i - 1]) {
  31.                                        tmp = unsorted[i];
  32.                                        unsorted[i] = unsorted[i - 1];
  33.                                        unsorted[i - 1] = tmp;
  34.  
  35.                                        hasSwapped = true;
  36.                                        }
  37.                                }     
  38.                                beg++;     //Increment beg by one
  39.  
  40.                 } while ((hasSwapped == true) && (beg != end)); //While no swaps have been made
  41.  
  42.                 return unsorted;
  43.              }
  44.  
  45.         /**
  46.         * Invokes a cocktail sort on the unsorted unsorted
  47.         * @param unsorted the unsorted integer set to be sorted
  48.         * @return a sorted version of the unsorted integer set
  49.         */
  50.              public Integer[] sort(Integer[] unsorted) {
  51.                 return cocktailSort(unsorted.clone());
  52.              }
  53. }
  54.  
  55.  

Copy & Paste


Comments


student_br 2008-10-29 17:43:53

i need to know how to do bidirectional bubble sort in javascript!!! any help???


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