Return to my Computer pages
Go to my home page


Bubble Sort

© Copyright 2000, Jim Loy

bubble sort animationThe bubble sort is very popular in computer classes. It is simple, and it amazes some people that it works at all. Essentially, you just keep going down the list that you want to sort, swapping elements that are out of order with each other. On the left, I am sorting the names of the planets. They start out in the order of their distance from the sun. And I sort them alphabetically. You start with the first two elements, and switch them if the second one should be first, then do the next two elements. The first time through the list, the last element is then in its proper place, so we don't have to sort that far down ever again. The second time through, the next to last one is in its proper place, etc. And the process is over when we go through the list (as much as was necessary) without encountering any element that is out of order.

Apparently, this method is called the bubble sort because some of the elements rise to their final position slowly, as a bubble might rise on the inside surface of a glass of soda pop. In general, the bubble sort is one of the slowest of all sorting methods. It is slower than any of the other sorting methods in its class (the slow n-squared sorts). There are simpler sorts that are faster. So the bubble sort is impractical by any standards. But it is educational, and so it survives. Incidentally, if the list is already sorted, the bubble sort is fastest of all sorts, as the sort goes once through the list, and then stops itself.


Return to my Computer pages
Go to my home page