Home arrow C++ arrow Page 3 - Bubble Sorts And C++
C++

Bubble Sorts And C++


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.

Author Info:
By: Steve Adcock
Rating: 4 stars4 stars4 stars4 stars4 stars / 81
February 13, 2002
TABLE OF CONTENTS:
  1. · Bubble Sorts And C++
  2. · Sorting algorithms
  3. · Blowing a bubble
  4. · The bubble sort code
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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

for(int i=0; i<7; i++)

// Loop through every cell (value) in array

for(int ii=0; ii<7; ii++)

// Compare the values and switch if necessary

if (NumList[ii] > NumList[ii + 1])

{

Swap = NumList[ii];

NumList[ii] = NumList[ii + 1];

NumList[ii + 1] = Swap;

}

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

// Print out sorted array

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

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

cout<< endl <<endl;

}

blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials