### Bubble Sort

**Bubble sort**, sometimes referred to as

**sinking sort**, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, it is too slow for practical use, even compared to insertion sort.

### Analysis

### Performance

Bubble sort has worst-case and average complexity both

*O*(*n*^{2}), where*n*is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of*O*(*n*log*n*). Even other*O*(*n*^{2}) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when*n*is large.
The only significant advantage that bubble sort has over most other implementations, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted is efficiently built into the algorithm. When the list is already sorted (best-case), the complexity of bubble sort is only

*O*(*n*). By contrast, most other algorithms, even those with better average-case complexity, perform their entire sorting process on the set and thus are more complex. However, not only does insertion sort have this mechanism too, but it also performs better on a list that is substantially sorted (having a small number of inversions).
Bubble sort should be avoided in the case of large collections. It will not be efficient in the case of a reverse-ordered collection.

### Rabbits and turtles

The positions of the elements in bubble sort will play a large part in determining its performance. Large elements at the beginning of the list do not pose a problem, as they are quickly swapped. Small elements towards the end, however, move to the beginning extremely slowly. This has led to these types of elements being named rabbits and turtles, respectively.

Various efforts have been made to eliminate turtles to improve upon the speed of bubble sort. Cocktail sort is a bi-directional bubble sort that goes from beginning to end, and then reverses itself, going end to beginning. It can move turtles fairly well, but it retains

*O(n*worst-case complexity. Comb sort compares elements separated by large gaps, and can move turtles extremely quickly before proceeding to smaller and smaller gaps to smooth out the list. Its average speed is comparable to faster algorithms like quicksort.^{2})### Step-by-step example

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in

**bold**are being compared. Three passes will be required.#### First Pass:

(

**5 1**4 2 8 ) (

**1 5**4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.

( 1

**5 4**2 8 ) ( 1

**4 5**2 8 ), Swap since 5 > 4

( 1 4

**5 2**8 ) ( 1 4

**2 5**8 ), Swap since 5 > 2

( 1 4 2

**5 8**) ( 1 4 2

**5 8**), Now, since these elements are already in order (8 > 5), algorithm does not swap them.

#### Second Pass:

(

**1 4**2 5 8 ) (

**1 4**2 5 8 )

( 1

**4 2**5 8 ) ( 1

**2 4**5 8 ), Swap since 4 > 2

( 1 2

**4 5**8 ) ( 1 2

**4 5**8 )

( 1 2 4

**5 8**) ( 1 2 4

**5 8**)

Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one

**whole**pass without

**any**swap to know it is sorted.

#### Third Pass:

(

**1 2**4 5 8 ) (

**1 2**4 5 8 )

( 1

**2 4**5 8 ) ( 1

**2 4**5 8 )

( 1 2

**4 5**8 ) ( 1 2

**4 5**8 )

( 1 2 4

**5 8**) ( 1 2 4

**5 8**)

#### Implementation

### Pseudocode implementation

The algorithm can be expressed as (0-based array):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 | procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped = false for i = 1 to n-1 inclusive do /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure |

### Optimizing bubble sort

The bubble sort algorithm can be easily optimized by observing that the n-th pass finds the n-th largest element and puts it into its final place. So, the inner loop can avoid looking at the last n-1 items when running for the n-th time:

1 2 3 4 5 6 7 8 9 10 11 12 13 | procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped = false for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i]) swapped = true end if end for n = n - 1 until not swapped end procedure |

More generally, it can happen that more than one element is placed in their final position on a single pass. In particular, after every pass, all elements after the last swap are sorted, and do not need to be checked again. This allows us to skip over a lot of the elements, resulting in about a worst case 50% improvement in comparison count (though no improvement in swap counts), and adds very little complexity because the new code subsumes the "swapped" variable:

To accomplish this in pseudocode we write the following:

1 2 3 4 5 6 7 8 9 10 11 12 13 | procedure bubbleSort( A : list of sortable items ) n = length(A) repeat newn = 0 for i = 1 to n-1 inclusive do if A[i-1] > A[i] then swap(A[i-1], A[i]) newn = i end if end for n = newn until n = 0 end procedure |

Alternate modifications, such as the cocktail shaker sort attempt to improve on the bubble sort performance while keeping the same idea of repeatedly comparing and swapping adjacent items.

### In practice

Although bubble sort is one of the simplest sorting algorithms to understand and implement, its

*O(n*complexity means that its efficiency decreases dramatically on lists of more than a small number of elements. Even among simple^{2})*O(n*sorting algorithms, algorithms like insertion sort are usually considerably more efficient.^{2})
Due to its simplicity, bubble sort is often used to introduce the concept of an algorithm, or a sorting algorithm, to introductory computer science students. However, some researchers such as Owen Astrachan have gone to great lengths to disparage bubble sort and its continued popularity in computer science education, recommending that it no longer even be taught.

The Jargon File, which famously calls bogosort "the archetypical [sic] perversely awful algorithm", also calls bubble sort "the generic

**bad**algorithm". Donald Knuth, in his famous book*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 then discusses.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. Experimental results such as those of Astrachan have also shown that insertion sort performs considerably better even on random lists. For these reasons many modern algorithm textbooks avoid using the bubble sort algorithm in favor of insertion sort.

Bubble sort also interacts poorly with modern CPU hardware. It requires at least twice as many writes as insertion sort, twice as many cache misses, and asymptotically more branch mis-predictions. Experiments by Astrachan sorting strings in Java show bubble sort to be roughly one-fifth as fast an insertion sort and 70% as fast as a selection sort.

In computer graphics it is popular for its capability to detect a very small error (like swap of just two elements) in almost-sorted arrays and fix it with just linear complexity (2n). For example, it is used in a polygon filling algorithm, where bounding lines are sorted by their x coordinate at a specific scan line (a line parallel to x axis) and with incrementing y their order changes (two elements are swapped) only at intersections of two lines. Bubble sort is a stable sort algorithm, like insertion sort.

### Variations

- Odd-even sort is a parallel version of bubble sort, for message passing systems.
- Cocktail sort is another parallel version of the bubble sort
- In some cases, the sort works from right to left (the opposite direction), which is more appropriate for partially sorted lists, or lists with unsorted items added to the end.

### Alone bubble sort

*Alone bubble sort*is a 1992 modification of the simple bubble sorting algorithm. Unlike the normal bubble sort where the loop resets after every performed swap of elements, in the alone bubble sort, the loop index only returns back by one step thus allowing the swapping to continue until a smaller value element in the array is reached. The following is the alone bubble realization (the algorithm only) in Pascal:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | f:=false; for x:=1 to max-1 do if a[x]>a[x+1] then begin if f=false then d:=x; f:=true; t:=a[x]; a[x]:=a[x+1]; a[x+1]:=t; if x>1 then dec(x,2) else x:=0; end else if f=true then begin x:=d; f:=false; end; |

### Debate over name

Bubble sort has occasionally been referred to as a "sinking sort".

However, in Donald Knuth's

*The Art of Computer Programming*, Volume 3:*Sorting and Searching*he states in section 5.2.1 'Sorting by Insertion', that [the value] "settles to its proper level" this method of sorting has often been called the*sifting*or*sinking*technique. Furthermore the*larger*values might be regarded as*heavier*and therefore be seen to progressively*sink*to the*bottom*of the list.
from : http://en.wikipedia.org/wiki/Bubble_sort

## Comments

## Post a Comment