# Thread: Bubble Sorting HELP WANTED !

1. ## Bubble Sorting HELP WANTED !

This bubble sort is kinda freaks me out. I got it two other ways with Number of comparisons = 48 and 81 but this one ....

Data item in original order

2 6 4 8 10 12 89 68 45 37

after passing 0 : 2 4 6 8 10 12 68 45 37 89
after passing 1 : 2 4 6 8 10 12 45 37 68
after passing 2 : 2 4 6 8 10 12 37 45
after passing 3 : 2 4 6 8 10 12 37
after passing 4 : 2 4 6 8 10 12
after passing 5 : 2 4 6 8 10
after passing 6 : 2 4 6 8
after passing 7 : 2 4 6
after passing 8 : 2 4

Data items in ascending order
2 4 6 8 10 12 37 45 68 89
Number of comparisons = 24
Press any key to continue

part of my code

for (int i = SIZE-1; i >= 0; i--)
{
for (int j = 1; j <= i; j++)
{
if (a[j-1] > a[j])
{
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
numberOfComp++; //// [b]this is what i want to change [b]
}

Regards.

__________________
Ok...

The Code is

#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

#include <cstdlib>
#include <ctime>

int main()
{

const int SIZE =10;
int a[SIZE]={2,6,4,8,10,12,89,68,45,37};
int numberOfComp=0,temp,counter=10,coun=0;

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

for(int b=0;b<SIZE;++b)
cout <<setw(4)<<a;

cout <<"\n\n";

for (int i = SIZE-1; i >= 0; i--)
{
for (int j = 1; j <= i; j++)
{
if (a[j-1] > a[j])
{
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
numberOfComp++;
}

if(coun<9)
{
cout << "after passing " << coun <<" : ";

for(int o=0;o<counter;o++)
cout << a[o]<<" ";

coun++;
counter--;
}
cout<<endl;

}

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

for(int j=0;j<SIZE;j++)
cout <<setw(4)<<a[j];

cout << "\nNumber of comparisons = " << numberOfComp<<endl;

return 0;

}

2. Use code tags.

What exactly do you want help with?

And also, don't double post, stick with one of the threads.

3. Don't double post! People will read the first one. Also don't use all caps in a title!

There is nothing wrong with your code. When I run it it prints 45 just like it should.

4. ## Ok !

Well the question was about the output.. I got it to print out 45 and 81 but this was my question how can i get the program to do the result below for number comparison?

Data items in ascending order
2 4 6 8 10 12 37 45 68 89
Number of comparisons = 24
Press any key to continue

5. Try creating a boolean variable, setting it to false at the start of each pass and if a swap is made, then set it to true. If no swaps are made, then the list is sorted.

The only way I managed to get 24 out of this data set is if I implement the above suggestion and also if I ignore all of the comparisons on the pass where all are in order.
[/edit]