At its simplest, bubble sort makes (n-1) passes through an array of n, each time comparing all adjacent pairs and then retiring the last element.
However, it does tend to be quite slow. (It is a quadratic sort.)
One easy optimization to make is to retire multiple elements at once. If there are k non-swaps at the end, (k+1) elements can be retired. Here is an optmized sorter vs. an un-optimized one:
However, that optimization really comes into play when the data is already semisorted:
Clearly, bubble sort is an adaptive sort. In fact, in this case bubble sort approaches being linear. This is actually pretty common among quadratic sorting algorithms. They tend to "waste time" with a lot of redundant checks, but that means that they're good at figuring out that things are already mostly sorted, and taking advantage of that.Bubble sort is a stable sort, so long as it's coded correctly. When two adjacent elements are tied, they must not be swapped.
Bubble sort isn't really used often. It's a good starting point, to talk about sorts, but that's about it.
Here is bubble sort working on a set of 200:
On to shaker sort...