Bubble Sort Algorithm
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
Pseudocode
procedure bubble_sort(array : list of sortable items, n : length of list)
do
swapped ← false
i ← 1
while i < n
if array[i - 1] > array[i]
swap array[i - 1] and array[i]
swapped ← true
end if
i ← i + 1
end while
while not swapped
end procedure
If no items are swapped during a pass through the outer loop (i.e., the variable swapped
remains false), then the array is already sorted and the algorithm can terminate.
Optimizing Bubble Sort
The bubble sort algorithm can be optimized by observing that the n-th pass finds the n-th largest element and puts it into its final place. Therefore the inner loop can avoid looking at the last n - 1 items when running for the n-th time:
procedure bubble_sort(array : list of sortable items, n : length of list)
do
swapped ← false
i ← 1
while i < n
if array[i - 1] > array[i]
swap array[i - 1] and array[i]
swapped ← true
end if
i ← i + 1
end while
n ← n - 1
while not swapped
end procedure
It is common for multiple elements to be placed in their final positions on a single pass. In particular, after every pass through the outer loop, all elements after the position of the last swap are sorted and do not need to be checked again. Taking this into account makes it possible to skip over many 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:
procedure bubble_sort(array : list of sortable items, n : length of list)
do
last_swap ← 0
i ← 1
while i < n
if array[i - 1] > array[i]
swap array[i - 1] and array[i]
last_swap ← i
end if
i ← i + 1
end while
n ← last_swap
while n > 1
end procedure
Example
The following example illustrates how an array changes after each pass through the outer loop of the bubble sort algorithm.
|
Original Array
|
23
|
12
|
18
|
42
|
29
|
37
|
15
|
10
|
|
|
Before 1st Pass
|
23
|
12
|
18
|
42
|
29
|
37
|
15
|
10
|
|
|
After 1st Pass
|
12
|
18
|
23
|
29
|
37
|
15
|
10
|
42
|
|
|
After 2nd Pass
|
12
|
18
|
23
|
29
|
15
|
10
|
37
|
42
|
|
|
After 3rd Pass
|
12
|
18
|
23
|
15
|
10
|
29
|
37
|
42
|
|
|
After 4th Pass
|
12
|
18
|
15
|
10
|
23
|
29
|
37
|
42
|
|
|
After 5th Pass
|
12
|
15
|
10
|
18
|
23
|
29
|
37
|
42
|
|
|
After 6th Pass
|
12
|
10
|
15
|
18
|
23
|
29
|
37
|
42
|
|
|
After 7th Pass
|
10
|
12
|
15
|
18
|
23
|
29
|
37
|
42
|
|
|
Sorted Array
|
10
|
12
|
15
|
18
|
23
|
29
|
37
|
42
|
|
Complexity
Time Complexity: O(n2)
Space Complexity: O(1)
Other O(n2) sorting algorithms, such as insertion sort, generally run faster than bubble sort (even with optimizations) and are no more complex. Therefore, bubble sort is not a practical sorting algorithm.
The only significant advantage that bubble sort has over most other sorting algorithms (but not insertion sort), is that the ability to detect that the list is sorted is built into the algorithm. When the list is already sorted (best-case), the complexity of bubble sort is only O(n).