Bubble Sort
Bubble sort is a very simple method that sorts the array elements
by repeatedly moving the largest element to the highest index
position of the array segment (in case of arranging elements in
ascending order).
In bubble sorting, consecutive adjacent pairs of elements in the
array are compared with each other.
If the element at the lower index is greater than the element at the
higher index, the two elements are interchanged so that the
element is placed before the bigger one.
This procedure of sorting is called bubble sorting because
elements ‘bubble’ to the top of the list.
Note that at the end of the first pass, the largest element in the list
will be placed at its proper
position
Technique
The basic methodology of the working of bubble sort is given as follows:
(a) In Pass 1, A[0] and A[1] are compared, then A[1] is compared with A[2], A[2] is compared with
A[3], and so on. Finally, A[N–2] is compared with A[N– 1]. Pass 1 involves n–1 comparisons and
places the biggest element at the highest index of the array.
(b) In Pass 2, A[0] and A[1] are compared, then A[1] is compared with A[2], A[2] is compared
with A[3], and so on. Finally, A[N–3] is compared with A[N–2]. Pass 2 involves n–2 comparisons
and places the second biggest element at the second highest index of the array.
(c) In Pass 3, A[0] and A[1] are compared, then A[1] is compared with A[2], A[2] is compared with
A[3], and so on. Finally, A[N–4] is compared with A[N–3]. Pass 3 involves n–3 comparisons and
places the third biggest element at the third highest index of the array.
(d) In Pass n–1, A[0] and A[1] are compared so that A[0]<A[1]. After this step, all the elements of
the array are arranged in ascending order.
Bubble Sort
Traverse a collection of elements
◦ Move from the front to the end
◦ “Bubble” the largest value to the end using pair-
wise comparisons and swapping
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 Swap 77
77 42 35 12 101 5
1 2 3 4 5 6
42 7735 Swap35
77 12 101 5
1 2 3 4 5 6
42 35 12 Swap 12
77 77 101 5
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
1 2 3 4 5 6
42 35 12 77 5 Swap 101
101 5
1 2 3 4 5 6
42 35 12 77 5 101
Largest value correctly placed
Items of Interest
Notice that only the largest value is
correctly placed
All other values are still out of order
So we need to repeat this process
1 2 3 4 5 6
42 35 12 77 5 101
Largest value correctly placed
Repeat “Bubble Up” How Many
Times?
If we have N elements…
And if each time we bubble an element, we
place it in its correct location…
Then we repeat the “bubble up” process N – 1
times.
This guarantees we’ll correctly place all N
elements.
“Bubbling” All the
Elements
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
Complexity of Bubble Sort
The complexity of any sorting algorithm depends upon the number of comparisons. In
bubble sort, we have seen that there are N–1 passes in total. In the first pass, N–1
comparisons are made to place the highest element in its correct position. Then, in
Pass 2, there are N–2 comparisons and the second highest element is placed in its
position. Therefore, to compute the complexity of bubble sort, we need to calculate
the total number of comparisons. It can be given as:
f(n) = (n – 1) + (n – 2) + (n – 3) + ..... + 3 + 2 + 1
f(n) = n (n – 1)/2
f(n) = n2/2 + O(n) = O(n2)
Therefore, the complexity of bubble sort algorithm is O(n2). It means the time
required to execute bubble sort is proportional to n2, where n is the total number of
elements in the array.
Already Sorted
Collections?
What if the collection was already sorted?
What if only a few elements were out of place
and after a couple of “bubble ups,” the collection
was sorted?
We want to be able to detect this and “stop
early”!
1 2 3 4 5 6
5 12 35 42 77 101
Using a Boolean “Flag”
We can use a boolean variable to determine if
any swapping occurred during the “bubble up.”
If no swapping occurred, then we know that the
collection is already sorted!
This boolean “flag” needs to be reset after each
“bubble up.”
Complexity of Optimized Bubble Sort
Algorithm
In the best case, when the array is already sorted, the optimized
bubble sort will take O(n) time. In the worst case, when all the
passes are performed, the algorithm will perform slower than the
original algorithm. In average case also, the performance will see
an improvement. Compare it with the complexity of original bubble
sort algorithm which takes O(n2) in all the cases.