Home arrow C++ arrow Page 4 - 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++ - The bubble sort code
(Page 4 of 5 )

Let's now run through each line in the bubble sorting code shown on the previous page.

#include <iostream>

using std::cout;

using std::endl;


Firstly, we must include the iostream header file so we can output the appropriate arrays to the console window.

#include <iomanip>

using std::setw;


We must also include the iomanip header file so we can set the spaces that will appear between the output to the console window. Notice how we use setw(3) when we displayed the array. The three refers to the number of spaces that our output will contain. If our output is less than three characters in length, then it's padded with spaces.

void main()

{

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

int Swap;


Next we define the entry-point function called main, and initialize an array (called NumList) with 8 indexes (ranging from 0 to 7). Within the curly braces we have explicitly defined the values of each index in the array. On the next line we define an integer variable called "Swap", which we will use to temporarily hold a value while we switch the positions of data in the NumList array.

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

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

{

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

}

cout<<"\n\n";


In the code above, we simply write a for loop to iterate through each array position and display its value. We output three spaces and add two line breaks after the value of the current index in the array it output.

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

The for loop above controls how many times we loop through the entire list of array values. Although our array has 8 values, we must keep the numbers of loops through the array to 7 (which is the arrays size minus one), as mentioned earlier.

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

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

{

Swap = NumList[ii];

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

NumList[ii + 1] = Swap;

}


The second loop shown above iterates through each index in the array and the if statement compares the value of the current index with the value of the next index in the array. If NumList[ii] > NumList[[ii + 1] means that if the value in the current position is greater than that in the next position, then we call Swap = NumList[ii], which assigns the value in the index NumList[ii] to the Swap integer variable so we won't lose that data when we do the switch.

We then assign NumList[ii] to the value of NumList[ii + 1]. Lastly, we assign the integer variable Swap (which is the current index in the array), to the next position in the index, which is NumList[ii + 1].

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

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

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

cout<< endl <<endl;


To finish of the sample code after the sort, we actually display the array. If you're running this C++ program on your own system, then you will see that all of the numbers are sorted appropriately. Also note that if two numbers are the same value (such as 4 and 4), then no swapping will occur, thus saving the time it would normally take to accomplish the swap.
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