Bubble Sort Algorithm, Code and Explaination

A computer program compare only two players at one time because that’s how the comparison operators work. This tunnel vision on the part of algorithms will be a recurring theme. Things may seem simple to us humans, but the algorithm can’t see the big picture and must, therefore, concentrate on the details and follow some simple rules.

Sorted Elements
Sorted Elements

The Bubble Sort algorithm in this article all involve two steps, executed over and over until the data is sorted:

  1. Compare two items.
  2. Swap two items, or copy one item.

However, each algorithm handles the details in a different way.

Bubble Sort

The bubble sort is notoriously slow, but it’s conceptually the simplest of the sorting algorithms and for that reason is a good beginning for our exploration of sorting techniques.

Bubble Sort on the Baseball Players

Imagine that you’re near-sighted (like a computer program) so that you can see only two of the baseball players at the same time, if they’re next to each other and if you stand very close to them. Given this impediment, how would you sort them? Let’s assume there are N players, and the positions they’re standing in are numbered from 0 on the left to N-1 on the right.

The bubble sort routine works like this: You start at the left end of the line and compare the two kids in positions 0 and 1. If the one on the left (in 0) is taller, you swap them. If the one on the right is taller, you don’t do anything. Then you move over one position and compare the kids in positions 1 and 2. Again, if the one on the left is taller, you swap them. This sorting process is shown in Figure 3.3.

Here are the rules you’re following:

  1. Compare two players.
  2. If the one on the left is taller, swap them.
  3. Move one position right.
    Bubble Sort Algorithm
    Bubble Sort Algorithm
    Bubble Sort Algorithm
    Bubble Sort Algorithm

    You continue down the line this way until you reach the right end. You have by no means finished sorting the kids, but you do know that the tallest kid is on the right. This must be true because, as soon as you encounter the tallest kid, you’ll end up swapping him (or her) every time you compare two kids, until eventually he (or she) will reach the right end of the line. This is why it’s called the bubble sort: As the algorithm progresses, the biggest items “bubble up” to the top end of the array. Figure 3.4 shows the baseball players at the end of the first pass.

    Bubble Sort Algorithm - End of the First Pass
    Bubble Sort Algorithm – End of the First Pass

    After this first pass through all the data, you’ve made N-1 comparisons and somewhere between 0 and N-1 swaps, depending on the initial arrangement of the players. The item at the end of the array is sorted and won’t be moved again.
    Now you go back and start another pass from the left end of the line. Again, you go toward the right, comparing and swapping when appropriate. However, this time you can stop one player short of the end of the line, at position N-2, because you know the last position, at N-1, already contains the tallest player. This rule could be stated as:

  1. When you reach the first sorted player, start over at the left end of the line.

You continue this process until all the players are in order.

The bubbleSort() method is only four lines long. Here it is, extracted from the listing:

Hope you enjoyed reading the article, share your ideas regarding the articles and be with codejow.