C++
  Home arrow C++ arrow Page 4 - 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 
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 / 64
    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++ - 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.

    More C++ Articles
    More By Steve Adcock


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek