1.1 BUBBLE SORT
The bubble sort is the oldest and simplest sorting method in
use. Unfortunately, it's also the slowest. The bubble sort works
by comparing each item in the list with the item next to it, and
swapping them if required. The algorithm repeats this process
until it makes a pass all the way through the list without
swapping any items (in other words, all items are in the correct
order). This causes larger values to "bubble" to the end of the
list while smaller values "sink" towards the beginning of the
list. The total number of comparisons, is (n - 1) + (n - 2)...(2) +
(1) = n(n - 1)/2 or O(n2). The bubble sort is generally
considered to be the most inefficient sorting algorithm in
common usage. Under best-case conditions (the list is already
sorted), the bubble sort can approach a constant O(n) level of
complexity. General-case is an abysmal O(n2). While the
insertion, selection, and shell sorts also have O(n2)
complexities, they are significantly more efficient than the
bubble sort. [1]. Don Knuth, in his famous The Art of Computer
Programming, concluded that "the bubble sort seems to have
nothing to recommend it, except a catchy name and the fact
that it leads to some interesting theoretical problems", some of
which he discusses therein. Bubble sort is asymptotically
equivalent in running time to insertion sort in the worst case,
but the two algorithms differ greatly in the number of swaps
necessary. Insertion sort needs only O(n) operations if the list is already sorted, whereas naïve
implementations of bubble sort (like the pseudocode below)
require O(n^2) operations. (This can be
reduced to O(n) if code is added to stop the
outer loop when the inner loop performs no swaps.) [2]. For
example, in [6] we find: ―The bubble sort is worse than
selection sort for a jumbled array—it will require many more
component exchanges—but it’s just as good as insertion sort
for a pretty well-ordered array. More important, it’s usually the
easiest one to write correctly.‖ Owen says that Bubble sort’s
prime virtue is that it is easy to implement, but whether it is
actually easier to implement than insertion or selection sort is
arguable [5]. Authors have tried to bring the Bubble Sort closer
to other sorts by using a new variation.