6.7. The Bubble Sort¶
The bubble sort makes multiple passes through a list. It compares adjacent items and exchanges those that are out of order. Each pass through the list places the next largest value in its proper place. In essence, each item “bubbles” up to the location where it belongs.
Figure 1 shows the first pass of a bubble sort. The shaded
items are being compared to see if they are out of order. If there are
n items in the list, then there are

Figure 1: bubbleSort
: The First Pass¶
At the start of the second pass, the largest value is now in place.
There are bubbleSort
function. It takes the list as a
parameter, and modifies it by exchanging items as necessary.
The exchange operation, sometimes called a “swap,” is slightly different in Python than in most other programming languages. Typically, swapping two elements in a list requires a temporary storage location (an additional memory location). A code fragment such as
temp = alist[i]
alist[i] = alist[j]
alist[j] = temp
will exchange the ith and jth items in the list. Without the temporary storage, one of the values would be overwritten.
In Python, it is possible to perform simultaneous assignment. The
statement a,b=b,a
will result in two assignment statements being
done at the same time (see Figure 2). Using simultaneous
assignment, the exchange operation can be done in one statement.
Lines 5-7 in ActiveCode 1 perform the exchange of the

Figure 2: Exchanging Two Values in Python¶
The following activecode example shows the complete bubbleSort
function working on the list
shown above.
The following animation shows bubbleSort
in action.
To analyze the bubble sort, we should note that regardless of how the
items are arranged in the initial list,
Pass |
Comparisons |
---|---|
1 |
|
2 |
|
3 |
|
… |
… |
A bubble sort is often considered the most inefficient sorting method since it must exchange items before the final location is known. These “wasted” exchange operations are very costly. However, because the bubble sort makes passes through the entire unsorted portion of the list, it has the capability to do something most sorting algorithms cannot. In particular, if during a pass there are no exchanges, then we know that the list must be sorted. A bubble sort can be modified to stop early if it finds that the list has become sorted. This means that for lists that require just a few passes, a bubble sort may have an advantage in that it will recognize the sorted list and stop. ActiveCode 2 shows this modification, which is often referred to as the short bubble.
Self Check
- [1, 9, 19, 7, 3, 10, 13, 15, 8, 12]
- This answer represents three swaps. A pass means that you continue swapping all the way to the end of the list.
- [1, 3, 7, 9, 10, 8, 12, 13, 15, 19]
- Very Good
- [1, 7, 3, 9, 10, 13, 8, 12, 15, 19]
- A bubble sort contines to swap numbers up to index position passnum. But remember that passnum starts at the length of the list - 1.
- [1, 9, 19, 7, 3, 10, 13, 15, 8, 12]
- You have been doing an insertion sort, not a bubble sort.
Q-4: Suppose you have the following list of numbers to sort: <br> [19, 1, 9, 7, 3, 10, 13, 15, 8, 12] which list represents the partially sorted list after three complete passes of bubble sort?