5.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 nβˆ’1 pairs of items that need to be compared on the first pass. It is important to note that once the largest value in the list is part of a pair, it will continually be moved along until the pass is complete.

../_images/bubblepass.png

Figure 1: Bubble Sort: The First PassΒΆ

At the start of the second pass, the largest value is now in place. There are nβˆ’1 items left to sort, meaning that there will be nβˆ’2 pairs. Since each pass places the next largest value in place, the total number of passes necessary will be nβˆ’1. After completing the nβˆ’1 passes, the smallest item must be in the correct position with no further processing required. ActiveCode 1 shows the complete bubble_sort 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 variable (an additional memory location). A code fragment such as

temp = a_list[i]
a_list[i] = a_list[j]
a_list[j] = temp

will exchange the i-th and j-th 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 i and (i+1)-th items using the three-step procedure described earlier. Note that we could also have used the simultaneous assignment to swap the items.

../_images/swap.png

Figure 2: Exchanging Two Values in PythonΒΆ

The following activecode example shows the complete bubble_sort function working on the list shown above.

The following animation shows bubble_sort in action.



To analyze the bubble sort, we should note that regardless of how the items are arranged in the initial list, nβˆ’1 passes will be made to sort a list of size n. Table 1 shows the number of comparisons for each pass. The total number of comparisons is the sum of the first nβˆ’1 integers. Recall that the sum of the first n integers is 12n2+12n. The sum of the first nβˆ’1 integers is 12n2+12nβˆ’n, which is 12n2βˆ’12n. This is still O(n2) comparisons. In the best case, if the list is already ordered, no exchanges will be made. However, in the worst case, every comparison will cause an exchange. On average, we exchange half of the time.

Table 1: Comparisons for Each Pass of Bubble SortΒΆ

Pass

Comparisons

1

nβˆ’1

2

nβˆ’2

3

nβˆ’3

…

…

nβˆ’1

1

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.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

Self Check

You have attempted 1 of 4 activities on this page