Bubble Sorting

• 06-12-2008
yukapuka
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????
• 06-12-2008
yukapuka
it works perfectly , but i dont quiet understand how it works the bubble sorting..
• 06-12-2008
tabstop
Quote:

Originally Posted by yukapuka
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.
• 06-12-2008
yukapuka
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
• 06-12-2008
tabstop
Quote:

Originally Posted by yukapuka
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.
• 06-13-2008
yukapuka
Thanks For The Advice , Problem Is Cleared...

:)
• 06-13-2008
iMalc
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;`
• 06-13-2008
yukapuka
i sensed something wasnt adding up in order, even tho the program calculated correctly,
that does make a big diff thanks IMALC !!!