C++
  Home arrow C++ arrow Page 3 - Bubble Sorts And C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Bubble Sorts And C++
By: Steve Adcock
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 62
    2002-02-13

    Table of Contents:
  • Bubble Sorts And C++
  • Sorting algorithms
  • Blowing a bubble
  • The bubble sort code
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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;

    }

    More C++ Articles
    More By Steve Adcock


     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT