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;
}