# Thread: Need Help With This Bubble Sort

1. ## Need Help With This Bubble Sort

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

Anyone can help me please email me at Shahram_Zia@yahoo.com

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

2. ## Here is the code it might help you a bit.

#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[b];

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;

}(B)numberOfComp++(B)

3. Don't double post. People will get to your thread when they get to it.

4. And PM'ing members to solicit their help is also frowned upon.

Popular pages Recent additions