|
|
|
|
|
|
|
We start writing the header of the BubbleSort( ) algorithm as a generic method:
public static <T extends Comparable<T>> void sort( T[] arr ) { int n = arr.length; for (int i = 0; i < n-1; i++) // Repeat n-1 times { // Compare every adjacent pair of element for ( int j = 0; j < n-1-i; j++ ) { if ( arr[j].compareTo(arr[j+1]) > 0 ) // If out of place exch( arr, j, j+1); // Swap list[j] and list[j+1] } } } |
Repeat the steps n−1 times:
public static <T extends Comparable<T>> void sort( T[] arr ) { int n = arr.length; for (int i = 0; i < n-1; i++) // Repeat n-1 times { // Compare every adjacent pair of element for ( int j = 0; j < n-1-i; j++ ) { if ( arr[j].compareTo(arr[j+1]) > 0 ) // If out of place exch( arr, j, j+1); // Swap list[j] and list[j+1] } } } |
In each iteration: compare every pair of adjacent element
public static <T extends Comparable<T>> void sort( T[] arr ) { int n = arr.length; for (int i = 0; i < n-1; i++) // Repeat n-1 times { // Compare every adjacent pair of element for ( int j = 0; j < n-1-i; j++ ) { if ( arr[j].compareTo(arr[j+1]) > 0 ) // If out of place exch( arr, j, j+1); // Swap list[j] and list[j+1] } } } |
If an pair is out of place, swap them:
public static <T extends Comparable<T>> void sort( T[] arr ) { int n = arr.length; for (int i = 0; i < n-1; i++) // Repeat n-1 times { // Compare every adjacent pair of element for ( int j = 0; j < n-1-i; j++ ) { if ( arr[j].compareTo(arr[j+1]) > 0 ) // If out of place exch( arr, j, j+1); // Swap arr[j] and arr[j+1] } } } |
DEMO: demo/14-sort/03-bubble-sort/BubbleSort.java + Demo.java (Integer) + Demo2.java (String)
|
|
|
|
|
|
Here is the baisc Bubble Sort algorithm (with spaces added so I can insert the improvement):
public static <T extends Comparable<T>> void sort( T[] arr ) { int n = arr.length; for (int i = 0; i < n-1; i++) // Repeat n-1 times { // Compare every adjacent pair of element for ( int j = 0; j < n-1-i; j++ ) { if ( arr[j].compareTo(arr[j+1]) > 0 ) // If out of place exch( arr, j, j+1); // Swap arr[j] and arr[j+1] } } } |
We first add a boolean variable to detect whether an iteration has perform at least 1 exch( ) operation:
public static <T extends Comparable<T>> void sort( T[] arr )
{
int n = arr.length;
boolean swapped;
for (int i = 0; i < n-1; i++) // Repeat n-1 times
{
// Compare every adjacent pair of element
for ( int j = 0; j < n-1-i; j++ )
{
if ( arr[j].compareTo(arr[j+1]) > 0 ) // If out of place
exch( arr, j, j+1); // Swap arr[j] and arr[j+1]
}
}
}
|
When the iteration begins, we set swapped = false:
public static <T extends Comparable<T>> void sort( T[] arr ) { int n = arr.length; boolean swapped; for (int i = 0; i < n-1; i++) // Repeat n-1 times { swapped = false; // Compare every adjacent pair of element for ( int j = 0; j < n-1-i; j++ ) { if ( arr[j].compareTo(arr[j+1]) > 0 ) // If out of place exch( arr, j, j+1); // Swap arr[j] and arr[j+1] } } } |
If there is an out-of-place pair, we set swapped = true to indicate that the array is not yet sorted:
public static <T extends Comparable<T>> void sort( T[] arr ) { int n = arr.length; boolean swapped; for (int i = 0; i < n-1; i++) // Repeat n-1 times { swapped = false; // Compare every adjacent pair of element for ( int j = 0; j < n-1-i; j++ ) { if ( arr[j].compareTo(arr[j+1]) > 0 ) // If out of place { exch( arr, j, j+1); // Swap arr[j] and arr[j+1] swapped = true; } } } } |
Finally: if at the end of the iteration, swapped == false, we konw that the array is sorted and exit the loop:
public static <T extends Comparable<T>> void sort( T[] arr ) { int n = arr.length; boolean swapped; for (int i = 0; i < n-1; i++) // Repeat n-1 times { swapped = false; // Compare every adjacent pair of element for ( int j = 0; j < n-1-i; j++ ) { if ( arr[j].compareTo(arr[j+1]) > 0 ) // If out of place { exch( arr, j, j+1); // Swap arr[j] and arr[j+1] swapped = true; } } if ( swapped == false) // No swaps --> sorted !! break; // exit loop } } |
Comment: the
"improved"
Bubble Sort has the
same
worst case
running time as the
basic version....
There are inputs that will
make the
"improved"
Bubble Sort
perform n-1 iterations
to complete the
sorting
|