Bubblesort

This horrible bubblesort algorithm tortured generations of students who take programming classes ;-).

In reality, bubblesort is an extremely simple algorithm the most distinctive feature of which is that this is most often
incorrectly implemented sorting algorithms in existence. Looks like few writes of bubblesort even open Knuth.

It is not only incorrectly implemented in popular web pages, it is also
often implemented incorrectly in textbooks and animations :-). Again, bubblesort
is probably the champion of incorrect implementations among simple sorting
algorithms

Bubblesort is probably the champion of incorrect implementations among
simple sorting algorithms

The idea is to swap adjacent elements, if the elements are out of order, moving up or down the data set. Usually multiple
passes are necessary. Each subsequent pass is shorter as elements after the last swap are already sorted (it is this
element that is often missed in "naive" implementations that dominate the web). It terminated as soon as no swaps were performed
during the pass. See
animation to get better idea of the details

On any set of data it is less efficient then Insertion sort so it has purely theoretical
value as it has some interesting properties. Like insertion sort it is a stable sorting algorithm.

For nearly sorted data this is one of the quickest algorithms available and, paradoxically, it usually beats Quicksort.
If the data already sorted it does exactly one pass and N comparison, again like the insertion sort.

It is probably one of the slowest algorithms on reversely sorted data.

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 or minimum element in the unsorted part is found and subsequent sort can be done on a subset
that is smaller then initial by one or more elements (the place of last swap is the boundary).

Speed with which largest element "sinks" is high -- he occupies its proper place in one pass, but the speed with which
small element "floats up" is slow: only one exchange is done on each path. That's why sometimes largest elements in
bubble sort are called "rabbits" and smallest "tortoises".

But it's details of the implementation that are presented are often 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 Donald Knuth's version:

As most implementations of bubblesort presented on the web and some implementations in the textbooks are incorrect it
is important to understand two properties of the correct implementation of bubblesort:

If there are no exchanges then the sorting is complete (the simplistic
implementation above does not have even this feature; it should generally be avoided).

Each next pass ends at the last swap of the previous pass. It does
not make any sense to continue comparing elements below this point.

Two properties of the correct implementation of bubblesort:

If there are no exchanges, then the sorting is complete .

Each next pass ends at the last swap of the previous pass.

Here is the reimplementation of the algorithm as it was discussed by Knuth:

There the valuable bound determines when the sorting is finished and it is
assigned the position of the last swap. The part of the code that "drags" maximum element to the bottom is sometimes called
"bubblemax" and can be coded as subroutines but that does not make much sense. You can also double this part of algorithm
adding "bubblemin" pass on the opposite direction to compensate for the slowness with which minimum elements are "bubbling
up" while max element is "sinking" in a single pass. This enhancement to bidirectional bubblesort is known also as shaker
sort. But the question is whether this enhancement worth the additional complexity as there are more efficient algorithms
of comparable complexity which achieve the same goals (first of all Mergesort).

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 few advantages of Bubblesort is that like insertion sort it is a natural algorithm to be used on lists.
It is also works very well on already sorted lists, or lists with just few permutations. If there is just one permutation
the algorithm requires just one pass to sort.

It is possible to increase the speed of Bubblesort, but only at the cost of increased complexity of the code. One trivial
enhancement is to make it symmetrical alternating passes in each direction. This variant is often called
cocktail sort (implementations at Wikipedia are incorrect) or shaker
short. It works much better is only works much better when some small and large items are swapped in the data set and concentrated
on the "wrong end".

The other possibility is shorten the size of the pass based on observation that the element before the first swap can
be used for the beginning of next pass: previous elements cannot change their places as they are in partial order. This
adds to total number of comparison as check for the first swaps will be performed for each swap. See
C++ Notes Algorithms More Bubble Sorts
for the implementation of this idea.

Incorrect implementation, but good animation including random, nearly sorted reverses and few unique data samples. It's
visible how inefficient bubblesort is on reversely sorted data array.

Several implementation in Perl, Python and C++ using swap subroutine. Does not too correct to me.

Bubble sort is the best first sorting algorithm to teach No, it isn't. I have written an
entire page about why bubble sort is a horrible
sorting algorithm to teach as some kind of first basic sorting.

Bubble sort is the easiest sorting algorithm to understand No, it isn't. Compared to both insertion sort
(the best O(n2) sorting algorithm) and selection sort, bubble sort is quite complicated for a beginner
programmer to understand. (More precisely, it's surprisingly difficult to understand why it works.) Both insertion
sort and selection sort are much closer to how people intuitively would sort a collection of things, which makes
them much easier to understand. Bubble sort is very exotic and has nothing to do with how people would intuitively
sort things.

Bubble sort is the easiest to implement No, it isn't. Both insertion sort and selection sort are at least
as easy to implement, and require approximately the same amount of code.

Bubble sort is efficient with small amounts of data. No, it isn't. Not compared with other O(n2)
sorting algorithms, one of the best of them being insertion sort. There exist no cases where bubble sort would be
faster than insertion sort. Insertion sort always beats bubble sort (this is actually rather easy to prove
mathematically). Since insertion sort is easier to understand and equally easy to implement, there just is no disadvantage
in using it rather than bubble sort.

It is not too often in the real world that you have to implement your own sort. Generally, whatever language you are
using has a library with this functionality built in. If the occasion does arise, however, it is important to understand
which algorithms are applicable in which situations. As with most choices, there is no absolute correct answer; there
are many trade offs to consider. When choosing an algorithm there are three things you should consider: performance,
overhead, and ease of implementation.

You should give equal consideration to each of these factors, disregarding any
one of them can lead to poor choices. It is common, for instance, for people to ignore the ease of implementation and
focus on the performance of the algorithm. The problem with this is that not every operation is critical. No one is
going to die if they songs on their play list do not get sorted quickly enough. Programmer time is more expensive than
run time as a professor of mine often said. In addition to this, some high performance algorithms can slower than simpler
algorithms due to overhead. If you are sorting 100 items, you can probably insertion sort them just as fast or faster
than you can heap sort them. The same would not be true with one million items; heap sort would be faster.

Once we consider all of the factors, you should find that no one algorithm is ideal in every case. There are some
algorithms, however, that are not ideal in any case. Unfortunately one of these algorithms is among the most popular:
bubble sort. Bubble sort is a very simple algorithm to implement and it has little overhead. The problem lies in its
performance. You might think that this conflicts with my earlier point that even simple, low performance algorithms
can be faster than others in the right situation. You also might think that bubble sort, being easy to implement, makes
up for its performance short comings. This would be true, if it were not the case that there are algorithms that are
equally simple to implement, require just as little overhead, and perform better in practice.

Insertion sort is one such algorithm. Like bubble sort it is an in place sort, and is just as easy to implement.
Both algorithms have the same time complexity (O notation), but in practice insertion sort performs better in most cases.
This being the case you may wonder why bubble sort is even around. Certainly if it is obsolete pages regarding its implementation
should be torn out of books and mentioning it should be punishable by a swift slap with a keyboard. Maybe not. When
I learned bubble sort it was as an example of how not to sort. In my non-expert opinion, it is equally important to
understand how NOT to do things as it is important to understand how TO do them. My point? Learn bubble sort, but never
use it.

Disadvantage: After the first pass it may not be necessary to examine the entire range of the array -- only from where
the lowest exchange occurred to where the highest exchange occurred. The parts above and below must already be sorted.
See below.

Bubble Sort -- Sort only necessary range

Here's a version of bubble sort that, on each pass, looks only at the region of array where more exchanges might be
necessary.

//========================================================= bubbleSortRange
// After a pass in bubble sort, it's only necessary to sort from just
// below the first exchange (small values may move lower)
// to just before the last exchange (largest values won't move higher).
// Everything that wasn't exchanged must be in the correct order.
// After each pass, the upper and lower bounds for the next pass are
// set from the positions of the first and last exchanges on prev pass.
void bubbleSortRange(int x[], int n) {
int lowerBound = 0; // First position to compare.
int upperBound = n; // First position NOT to compare.
//--- Continue making passes while there is a potential exchange.
while (lowerBound <= upperBound) {
int firstExchange = n; // assume impossibly high index for low end.
int lastExchange = -1; // assume impossibly low index for high end.
//--- Make a pass over the appropriate range.
for (int i=lowerBound; i<upperBound; i++) {
if (x[i] > x[i+1]) {
//--- exchange elements
int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp;
//--- Remember first and last exchange indexes.
if (i<firstExchange) { // true only for first exchange.
firstExchange = i;
}
lastExchange = i;
}
}
//--- Prepare limits for next pass.
lowerBound = firstExchange-1;
if (lowerBound < 0) {
lowerBound = 0;
}
upperBound = lastExchange;
}
}//end bubbleSortRange

The text from the above example can be selected, copied, and pasted into an editor.

Disadvantage: Notice that the largest unsorted element will always be moved all the way to its correct position in the
array, but small elements are shifted down by only one place on each pass.

Other bubble sorts

Note that in all these sorts the part that is sorted grows at only one end of the array. The ability to quit early is
not symmetrical. The extreme values move all the way to the end in one direction, but only one place in the other direction.
The algorithm can be improved by alternately "bubbling" in opposite directions.

The constructor and the insert() and display() methods of this class are similar to those we've
seen before. However, there's a new method: bubbleSort(). When this method is invoked from main(),
the contents of the array are rearranged into sorted order.

The main() routine inserts 10 items into the array in random order, displays the array, calls bubbleSort()
to sort it, and then displays it again. Here's the output:

77 99 44 55 22 88 11 0 66 33
0 11 22 33 44 55 66 77 88 99

The bubbleSort() method is only four lines long. Here it is, extracted from the listing:

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.

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.

FAIR USE NOTICE This site contains
copyrighted material the use of which has not always been specifically
authorized by the copyright owner. We are making such material available
in our efforts to advance understanding of environmental, political,
human rights, economic, democracy, scientific, and social justice
issues, etc. We believe this constitutes a 'fair use' of any such
copyrighted material as provided for in section 107 of the US Copyright
Law. In accordance with Title 17 U.S.C. Section 107, the material on
this site is distributed without profit exclusivly for research and educational purposes. If you wish to use
copyrighted material from this site for purposes of your own that go
beyond 'fair use', you must obtain permission from the copyright owner.

ABUSE: IPs or network segments from which we detect a stream of probes might be blocked for no
less then 90 days. Multiple types of probes increase this period.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be
tracked by Google please disable Javascript for this site. This site is perfectly usable without
Javascript.

Original materials copyright belong
to respective owners. Quotes are made for educational purposes only
in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains
copyrighted material the use of which has not always been specifically
authorized by the copyright owner. We are making such material available
to advance understanding of computer science, IT technology, economic, scientific, and social
issues. We believe this constitutes a 'fair use' of any such
copyrighted material as provided by section 107 of the US Copyright Law according to which
such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free)
site written by people for whom English is not a native language. Grammar and spelling errors should
be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development
of this site and speed up access. In case softpanorama.org is down currently there are
two functional mirrors: softpanorama.info (the fastest) and softpanorama.net.

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or
referenced source) and are
not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with.We do not warrant the correctness
of the information provided or its fitness for any purpose.