|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
Softpanorama Search
|
| See Also | Recommended Books | Recommended Links | Shaker sort | |
| Animations | Test suits | Donald Knuth | Humor | Etc |
This horrible bubblesort algorithm ;-). BubbleSort is an extremely simple algorithm that is often implemented incorrectly is textbooks and animations ;-). The idea is to swap adjacent elements, if the elements are out of order. It terminated as soon as no swaps were performed during the pass. But an interesting and often overlooked feature of the algorithm is that the last swap signify the boundary of the already sorted part of the array. In this sense bubblesort is similar to selection sort as on each pass the maximum of the unsorted part is found.
But it's details of the implementation that are often are incorrect :-). For example Robert Sedgewick's implementation Algorithms in Java, Third Edition, Parts 1-4: Fundamentals, Data Structures, Sorting, Searching is definitely wrong (too primitive to be exact) and does not corresponds to Knuth's version:
static void bubble(ITEM[] a, int l, int r)
{ for (int i = l; i < r; i++)
for (int j = r; j > i; j--)
compExch(a, j-1, j);
}
First of all, if there are no exchanges then the sorting is complete (the simplistic implementation above does not have this feature; it should generally be avoided). Here is the reimplementation of the algorithms as it was discusses by Knuth:
void bubbleSort(int a[], int n)
/* Sorts in increasing order the array A of the size N */
{
int k;
int bound = n-1;
int t;
int last_swap;
while (bound) {
last_swap = 0;
for ( k=0; k<bound; k++ )
t = a[k]; /* t is a maximum of A[0]..A[k] */
if ( t > a[k+1] ) {
a[k] = a[k+1]; a[k+1] = t; /*swap*/
last_swap = k; /* mark the last swap position */
}//if
}//for
bound=last_swap; /*elements after bound already sorted */
}//while
} // bubbleSort
If you were to watch a graphical representation of the sorting occurring, you would see that each path the smallest element gets "bubbled" up to the top, while the largest element is sinking to the bottom.
One of the primary advantages of BubbleSort is that it is a natural algorithm to be used on lists. It is also works very well on already sorted lists, or list with just one permutation doing just one pass on them. It is a stable sorting algorithm.
It is possible to increase the speed of BubbleSort, but only at the cost of increased complexity of the code. See the paper by Shad Stafford BubbleSort for some opportunities. The most common of enhancement is bidirectional bubblesort know also as shaker sort. It works in both directions. Shakersort is also pretty efficient for "almost sorted" sequences (with small percentage of permutations). But this enhancement does not worth additional complexity as there are more efficient algorithms of comparable complexity which achieve the same goals (mergesort, etc).
|
|||||||
Visualizers >
Bubble sort (double direction)
Definition: A variant of bubble sort which compares each adjacent pairs of items in a list in turn, swapping them if necessary, and alternately passes through the list from the beginning to the end then from the end to the beginning until no swaps are done.
Also known as cocktail shaker sort, shaker sort, double-direction bubble sort.
See also sort.
Note: Complexity is O(n2) for arbitrary data, but approaches O(n) if the list is nearly in order at the beginning. Bidirectional bubble sort usually does better than bubble sort since at least one item is moved forward or backward to its place in the list with each pass. Bubble sort moves items forward into place, but can only move items backward one location each pass.
Bidirectional Bubble Sort Algorithm Demo
Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Disclaimer:
Last modified: August 15, 2009