Skip over navigation

Contents

Bubble Sort

Bubble Sort

Table of Contents

Sorting Terms

Because of bubble sort's simplicity, it is one of the oldest sorts known to man. It based on the property of a sorted list that any two adjacent elements are in sorted order. In a typical iteration of bubble sort each adjacent pair of elements is compared, starting with the first two elements, then the second and the third elements, and all the way to the final two elements. Each time two elements are compared, if they are already in sorted order, nothing is done to them and the next pair of elements is compared. In the case where the two elements are not in sorted order, the two elements are swapped, putting them in order.

Consider a set of data: 5 9 2 8 4 6 3. Bubble sort first compares the first two elements, the 5 and the 6. Because they are already in sorted order, nothing happens and the next pair of numbers, the 6 and the 2 are compared. Because they are not in sorted order, they are swapped and the data becomes: 5 2 9 8 4 6 3. To better understand the "bubbling" nature of the sort, watch how the largest number, 9, "bubbles" to the top in the first iteration of the sort.

(5 9) 2 8 4 6 3 --> compare 5 and 9, no swap 5 (9 2) 8 4 6 3 --> compare 9 and 2, swap 5 2 (9 8) 4 6 3 --> compare 9 and 8, swap 5 2 8 (9 4) 6 3 --> compare 9 and 4, swap 5 2 8 4 (9 6) 3 --> compare 9 and 6, swap 5 2 8 4 6 (9 3) --> compare 9 and 3, swap 5 2 8 4 6 3 9 --> first iteration complete

The bubble sort got its name because of the way the biggest elements "bubble" to the top. Notice that in the example above, the largest element, the 9, got swapped all the way into its correct position at the end of the list. As demonstrated, this happens because in each comparison, the larger element is always pushed towards its place at the end of the list.

In the second iteration, the second-largest element will be bubbled up to its correct place in the same manner:

  • (5 2) 8 4 6 3 9 --> compare 5 and 2, swap
  • 5 (2 8) 4 6 3 9 --> compare 2 and 8, no swap
  • 5 2 (8 4) 6 3 9 --> compare 8 and 4, swap
  • 5 2 4 (8 6) 3 9 --> compare 8 and 6, swap
  • 5 2 4 6 (8 3) 9 --> compare 8 and 3, swap
  • 5 2 4 6 3 8 9 --> no need to compare last two In the next pass through the list, the 6 would bubble up to its position, then the 5, the 4, the 3, and finally the 2. Here is a complete trace of the bubble sort algorithm on a ten element data set:
    8 9 3 5 6 4 2 1 7 0
    8 9 3 5 6 4 2 1 7 0
    8 3 9 5 6 4 2 1 7 0
    8 3 5 9 6 4 2 1 7 0
    8 3 5 6 9 4 2 1 7 0
    8 3 5 6 4 9 2 1 7 0
    8 3 5 6 4 2 9 1 7 0
    8 3 5 6 4 2 1 9 7 0
    8 3 5 6 4 2 1 7 9 0
    8 3 5 6 4 2 1 7 0 9
    3 8 5 6 4 2 1 7 0 9
    3 5 8 6 4 2 1 7 0 9
    3 5 6 8 4 2 1 7 0 9
    3 5 6 4 8 2 1 7 0 9
    3 5 6 4 2 8 1 7 0 9
    3 5 6 4 2 1 8 7 0 9
    3 5 6 4 2 1 7 8 0 9
    3 5 6 4 2 1 7 0 8 9
    3 5 6 4 2 1 7 0 8 9
    3 5 6 4 2 1 7 0 8 9
    3 5 4 6 2 1 7 0 8 9
    3 5 4 2 6 1 7 0 8 9
    3 5 4 2 1 6 7 0 8 9
    3 5 4 2 1 6 7 0 8 9
    3 5 4 2 1 6 0 7 8 9
    3 5 4 2 1 6 0 7 8 9
    3 4 5 2 1 6 0 7 8 9
    3 4 2 5 1 6 0 7 8 9
    3 4 2 1 5 6 0 7 8 9
    3 4 2 1 5 6 0 7 8 9
    3 4 2 1 5 0 6 7 8 9
    3 4 2 1 5 0 6 7 8 9
    3 2 4 1 5 0 6 7 8 9
    3 2 1 4 5 0 6 7 8 9
    3 2 1 4 5 0 6 7 8 9
    3 2 1 4 0 5 6 7 8 9
    2 3 1 4 0 5 6 7 8 9
    2 1 3 4 0 5 6 7 8 9
    2 1 3 4 0 5 6 7 8 9
    2 1 3 0 4 5 6 7 8 9
    1 2 3 0 4 5 6 7 8 9
    1 2 3 0 4 5 6 7 8 9
    1 2 0 3 4 5 6 7 8 9
    1 2 0 3 4 5 6 7 8 9
    1 0 2 3 4 5 6 7 8 9
    0 1 2 3 4 5 6 7 8 9

Follow Us