this is the exact problem:

Write a routine to sort the ten-number list shown in ‘Sample Screen Output’ below, and then make the following modifications to improve the performance of the bubble sort:

Sample Screen Output
Data items in original order
2 6 4 8 10 12 89 68 45 37
After pass 0: 2 4 6 8 10 12 68 45 37 89
After pass 1: 2 4 6 8 10 12 45 37 68
After pass 2: 2 4 6 8 10 12 37 45
After pass 3: 2 4 6 8 10 12 37
After pass 4: 2 4 6 8 10 12
After pass 5: 2 4 6 8 10
After pass 6: 2 4 6 8
After pass 7: 2 4 6
After pass 8: 2 4
Data items in ascending order
2 4 6 8 10 12 37 45 68 89
Number of comparisons = 45


This is what i have done so far, it gives all the data in ascending order but i cant figure out how to print after each pass as shown above.

#include <iostream>
using namespace std;

#include <iomanip>
using std::setw;

void main()
{
	int nums[10] = {2,6,4,8,10,12,89,68,45,37};
	int Swap;

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

	for(int ctr=0; ctr<10; ctr++)
		{
		cout<< setw(4) <<nums[ctr];
		}
	cout<<"\n\n";

	

    for(int i=0; i<10; i++)
		for(int ii=0; ii<10; ii++)


	if (nums[ii] > nums[ii + 1])
	{
    	Swap = nums[ii];
    	nums[ii] = nums[ii + 1];

	    nums[ii + 1] = Swap;
	}


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

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

    cout<< setw(4) << nums[iii];

    cout<< endl <<endl;

}

i cant figure out how to print after each pass as shown above.

After each swap is done, use cout to display the values stored in the list at that particular juncture.

and don't be afraid to make liberal use of { and } in the loops so that you can add additional lines with the loops. Even though not required with one-line statements they braces are good to use so that you can easily change the code.

for(int i=0; i<10; i++)
{
    for(int ii=0; ii<10; ii++)
    {
         if (nums[ii] > nums[ii + 1])
         {
            Swap = nums[ii];
            nums[ii] = nums[ii + 1];
            nums[ii + 1] = Swap;
         }
    }
}

thanks for reply, it works fine but theres a couple more things:

1) how to i calculate the number of of comparisons made and then print it?

2)im am trying to output in the form

After pass 1: 2 4 6 8 10 12 45 37 68

instead i can only manage to get
After pass 1:
2 4 6 8 10 12 45 37 68

I want it as a single line output,. I have tried fiddling around with cout statement into one line but wouldn't work.

thanks for reply, it works fine but theres a couple more things:

1) how to i calculate the number of of comparisons made and then print it?

Set a value to 0 before the loops and increment it immediately before the comparison. When the loop is done, output it.

2)im am trying to output in the form

After pass 1: 2 4 6 8 10 12 45 37 68

instead i can only manage to get
After pass 1:
2 4 6 8 10 12 45 37 68

I want it as a single line output,. I have tried fiddling around with cout statement into one line but wouldn't work.

Don't output \n at the end of the statement.

This article has been dead for over six months. Start a new discussion instead.