Thread: Need Help With This Bubble Sort

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    13

    Question 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 [email protected]

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

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    13

    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)
    Ok...

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Don't double post. People will get to your thread when they get to it.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    And PM'ing members to solicit their help is also frowned upon.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  3. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  4. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM