Bubble Sorting

This is a discussion on Bubble Sorting within the C++ Programming forums, part of the General Programming Boards category; Can Somebody Explain this to me??? Code: #include <iostream> #include <iomanip> using namespace std; int main() { const int NUM_QUIZZES ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    48

    Bubble Sorting

    Can Somebody Explain this to me???
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
      const int NUM_QUIZZES = 3;
    
      int grade[NUM_QUIZZES];     //The array to store the quiz grades
       int quiz,            // Subscript for the array grade[]
           temp,            // For swapping array elements
           pass,            // The number of the pass
           limit;           // Keeps track of how far to go on a pass
    
       // Get the grades
    
       cout << "Please enter " << NUM_QUIZZES
            << " integer quiz grades." << endl << endl;
    
       for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
       {
         cout << endl;
         cout << "Enter grade for quiz " << quiz + 1 << ": ";
         cin >> grade[quiz];
       }
       // Display the quiz grades
    
       cout << endl << endl;
       cout << "The grades you entered are as follows:" << endl;
    
       for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
         cout << setw(6) << grade[quiz];
    
       cout << endl;
    
       // Do the bubble sort
    
       limit = NUM_QUIZZES - 2; 
       
    
    
       for (pass = 1; pass <= NUM_QUIZZES - 1; ++pass)
       {
    	   cout<<"Pass: "<<pass<<endl;//1//2
         for (quiz = 0; quiz <= limit; ++quiz)
           if (grade[quiz] > grade[quiz + 1])
           {
             temp = grade[quiz];
             grade[quiz] = grade[quiz + 1];
             grade[quiz + 1] = temp;
           }
         limit;
    	 
       }
       // Display the sorted quiz grades
    
       cout << endl << endl;
       cout << "The grades in increasing order are as follows:" << endl;
       for (quiz = 0; quiz < NUM_QUIZZES; ++quiz)
         cout << setw(6) << grade[quiz];
    
       cout << endl;
    
       return 0;
    }

    why "num-quizzes minus 2", and why the "limit;" and why the "num quiz -1", under the do bubble sort comment????

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    48
    it works perfectly , but i dont quiet understand how it works the bubble sorting..

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by yukapuka View Post
    it works perfectly , but i dont quiet understand how it works the bubble sorting..
    So run through the algorithm with some numbers and see how it works.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    48
    i am not very familiar with what u mean by running algorithm with some numbers ???
    bear in mind your talking to a beginner so please elaborate with an example or something

    thanks

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by yukapuka View Post
    i am not very familiar with what u mean by running algorithm with some numbers ???
    bear in mind your talking to a beginner so please elaborate with an example or something

    thanks
    Pick some numbers, and pretend you are the machine, and do all the steps and see what happens.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    48
    Thanks For The Advice , Problem Is Cleared...

    Last edited by yukapuka; 06-13-2008 at 01:49 AM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    The sort algorithm is doing a lot more comparisons than it's supposed to because of a bug with this line:
    Code:
         limit;
    It should be:
    Code:
         --limit;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    48
    i sensed something wasnt adding up in order, even tho the program calculated correctly,
    that does make a big diff thanks IMALC !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sorting a 2-d array
    By stodd04 in forum C Programming
    Replies: 5
    Last Post: 03-17-2005, 12:40 PM
  2. bubble sorting multiple strings
    By jibbles in forum C Programming
    Replies: 10
    Last Post: 10-11-2003, 11:48 PM
  3. help with my bubble sorting of arrays
    By Matt in forum C Programming
    Replies: 1
    Last Post: 12-11-2001, 03:43 PM
  4. bubble sorting in linked list
    By anu in forum C Programming
    Replies: 4
    Last Post: 10-17-2001, 06:58 PM
  5. bubble sorting in linked list
    By anu in forum C Programming
    Replies: 0
    Last Post: 10-17-2001, 03:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21