The bubble sort algorithm is the simplest known method of sorting an array of data. In this article Steve describes the concepts behind several sorting methods, focusing on bubble sorts in general. He also creates a C++ sample application that bubble sorts an array of integers.

Bubble Sorts And C++ - Blowing a bubble (Page 3 of 5 )

Let's say that we have an array of numbers like this:

5, 34, 32, 25, 75, 42, 22, 2

We are tasked with sorting these eight numbers from the left in ascending order, which is from the smallest to the largest. Obviously these numbers aren't yet in order, so we need to write a little bit of code to accomplish this task. First though, let's actually describe what a bubble sort on our eight numbers will do.

To begin with, we look at the first two numbers, 5 and 34. Since we are sorting in ascending order, the smallest number should be first. Is it? Yes, so for the first two numbers no changes need to be made. Next, we look at the second and third numbers, 34 and 32. Oops, a change needs to be made here right? Since 34 comes after 32, we need to switch the two numbers. Our numbers now look like this:

5, 32, 34, 25, 75, 42, 22, 2

Continuing through the numbers, we arrive at 34 and 25, and yes, we need to switch them. Next we look at 34 and 75: no changes are needed here, so we move down the list. 75 and 42 need changing, 75 and 22 need changing and 75 and 2 need changing. So, our set of numbers now looks like this:

5, 32, 25, 34, 42, 22, 2, 75

We've gone through the list once and our numbers still aren't sorted. What do we do now? Well, we loop through the list again and perform exactly the same procedure.

Notice that the original list of numbers had the smallest number, 2, at the bottom, so we need to loop through the list (number of items in the array) – 1 times. Why you ask? Well when we loop through the array, we instruct the program to look at the data in current array position and also the data in the current array position + 1. It is better explained with some accompanying code, so let's take a look at some code that would pull this sort of sorting off.

Coding a bubble sort

There are a number of different programming languages that we can use to demonstrate a bubble-sorting algorithm, but for this article I have selected C++, which is commonly used to perform complex data manipulation routines.

If you have a C++ compiler, you can even copy and paste the code shown below right into a text file, compile it, and see it run right before your eyes. Let's take a look at the entire code example first, and then I'll explain line by line what the code means.

#include <iostream>

using std::cout;

using std::endl;

#include <iomanip>

using std::setw;

void main()

{

int NumList[8] = {5, 34, 32, 25, 75, 42, 22, 2};

int Swap;

cout<<"Data in original order: \n";

// This line loops through the array and displays all values, as is

for(int ctr=0; ctr<8; ctr++)

{

cout<< setw( 3 ) <<NumList[ctr];

}

cout<<"\n\n";

// Loop through the array one less than its total cell count