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.
The Bubble Sort algorithm in this article all involve two steps, executed over and over until the data is sorted:
- Compare two items.
- Swap two items, or copy one item.
However, each algorithm handles the details in a different way.
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:
- Compare two players.
- If the one on the left is taller, swap them.
- Move one position right.
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.
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:
- 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:
public void bubbleSort()
int out, in;
for(out=nElems-1; out>1; out--) // outer loop (backward)
for(in=0; in<out; in++) // inner loop (forward)
if( a[in] > a[in+1] ) // out of order?
swap(in, in+1); // swap them
} // end bubbleSort()
Hope you enjoyed reading the article, share your ideas regarding the articles and be with codejow.