Thread: Bubble Sorting HELP WANTED !

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

    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;

    }
    Ok...

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Use code tags.

    What exactly do you want help with?

    And also, don't double post, stick with one of the threads.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    13

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

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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.

    [edit]
    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]
    Last edited by XSquared; 03-12-2003 at 08:17 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sorting a 2-d array
    By stodd04 in forum C Programming
    Replies: 5
    Last Post: 03-17-2005, 01:40 PM
  2. Bubble sorting singly link list
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 09-08-2004, 05:44 AM
  3. bubble sorting multiple strings
    By jibbles in forum C Programming
    Replies: 10
    Last Post: 10-11-2003, 11:48 PM
  4. help with my bubble sorting of arrays
    By Matt in forum C Programming
    Replies: 1
    Last Post: 12-11-2001, 04:43 PM
  5. bubble sorting in linked list
    By anu in forum C Programming
    Replies: 0
    Last Post: 10-17-2001, 03:21 PM