Double Bubble Sort Algorithm Fails to Sort AT ALL

Discussion in 'C++' started by Protoman, Apr 2, 2006.

  1. Protoman

    Protoman Guest

    Why doesn't my double bubble sort algorithm sort; it just spits the
    numbers out, int the exact same order that I inputted them.

    Code:

    void swap(int& i,int& j)
    {
    i^=j;
    j^=i;
    i^=j;
    }

    void doubleBubbleSort(int *array, int length)
    {
    int i,j;
    for(i=0; i<length--;i++)
    {
    if(array>array[i++])
    for(j=i;j>=0&&(array[j]>array[j++]);j--)
    swap(array[j],array[j++]);
    }
    }

    Any help? Thanks!!!!!!!!
    Protoman, Apr 2, 2006
    #1
    1. Advertising

  2. * Protoman wroteth:
    > Why doesn't my double bubble sort algorithm sort; it just spits the
    > numbers out, int the exact same order that I inputted them.
    >
    > Code:
    >
    > void swap(int& i,int& j)
    > {
    > i^=j;
    > j^=i;
    > i^=j;
    > }


    That should work, but is unnecessarily obfuscated.


    > void doubleBubbleSort(int *array, int length)
    > {
    > int i,j;
    > for(i=0; i<length--;i++)
    > {
    > if(array>array[i++])
    > for(j=i;j>=0&&(array[j]>array[j++]);j--)
    > swap(array[j],array[j++]);
    > }
    > }
    >
    > Any help? Thanks!!!!!!!!


    I'd like to write that the direct cause is lack of proper indentation,
    which in most such cases it is, but unfortunately not here.

    To fix the code, adhere to this simple rule:

    Do not change loop control variables except in the loop header.



    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Apr 2, 2006
    #2
    1. Advertising

  3. Protoman

    loufoque Guest

    Protoman wrote :

    > void doubleBubbleSort(int *array, int length)
    > {
    > int i,j;
    > for(i=0; i<length--;i++)
    > {
    > if(array>array[i++])
    > for(j=i;j>=0&&(array[j]>array[j++]);j--)
    > swap(array[j],array[j++]);
    > }
    > }


    Please try to code in C++, not in C.
    loufoque, Apr 2, 2006
    #3
  4. Protoman

    Heinz Ozwirk Guest

    "Protoman" <> schrieb im Newsbeitrag news:...

    > if(array>array[i++])
    > for(j=i;j>=0&&(array[j]>array[j++]);j--)
    > swap(array[j],array[j++]);


    Each of these lines shows undefined behaviour.

    Heinz
    Heinz Ozwirk, Apr 2, 2006
    #4
  5. Protoman

    Protoman Guest

    loufoque wrote:
    > Protoman wrote :
    >
    > > void doubleBubbleSort(int *array, int length)
    > > {
    > > int i,j;
    > > for(i=0; i<length--;i++)
    > > {
    > > if(array>array[i++])
    > > for(j=i;j>=0&&(array[j]>array[j++]);j--)
    > > swap(array[j],array[j++]);
    > > }
    > > }

    >
    > Please try to code in C++, not in C.


    How is this C and not C++?
    Protoman, Apr 2, 2006
    #5
  6. Protoman

    Jack Klein Guest

    On Sun, 02 Apr 2006 12:17:04 +0200, loufoque
    <> wrote in comp.lang.c++:

    > Protoman wrote :
    >
    > > void doubleBubbleSort(int *array, int length)
    > > {
    > > int i,j;
    > > for(i=0; i<length--;i++)
    > > {
    > > if(array>array[i++])
    > > for(j=i;j>=0&&(array[j]>array[j++]);j--)
    > > swap(array[j],array[j++]);
    > > }
    > > }

    >
    > Please try to code in C++, not in C.


    Except for the undefined behavior, which Heinz already pointed out in
    a separate reply, this code is C++, just not valid C++ due to that
    undefined behavior.

    Given this part of the OP's code that you failed to quote:

    > void swap(int& i,int& j)
    > {
    > i^=j;
    > j^=i;
    > i^=j;
    > }


    ....it is not, and cannot, be C code at all.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Apr 2, 2006
    #6
  7. "Protoman" <> wrote in message
    news:...
    : Why doesn't my double bubble sort algorithm sort; it just spits the
    : numbers out, int the exact same order that I inputted them.
    :
    : Code:
    :
    : void swap(int& i,int& j)
    : {
    : i^=j;
    : j^=i;
    : i^=j;
    : }
    Unnecessarily complex, and likely to be executed slower
    than:
    int temp = i;
    i=j;
    j=temp;
    or better: std::swap(i,j).
    What can make sense for hardware registers isn't
    to be blindly applied to higher-level languages.

    : void doubleBubbleSort(int *array, int length)
    : {
    : int i,j;
    : for(i=0; i<length--;i++)
    : {
    : if(array>array[i++])
    : for(j=i;j>=0&&(array[j]>array[j++]);j--)
    : swap(array[j],array[j++]);
    : }
    : }
    :
    : Any help? Thanks!!!!!!!!

    It is illegal in C or C++ to re-use, within the
    same expression, a value that is modified therein.
    (read about "sequence points" if you want to learn more).
    This aside, when you write " array>array[i++] ",
    which side of the expression do you expect to refer
    to array[i+1] ??

    If taking syntactic shortcuts wasn't already bad
    enought for code maintenance, it is even worse when
    you don't fully understand language rules...


    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Apr 3, 2006
    #7
  8. Protoman

    loufoque Guest

    Protoman wrote :

    > How is this C and not C++?


    C++ allows you to do it in a better and more generic way. Just look at
    how std::sort works.
    loufoque, Apr 3, 2006
    #8
  9. In message <b52a1$4430e886$3e028af2$>, Ivan Vecerina
    <> writes
    >"Protoman" <> wrote in message
    >news:...
    >: Why doesn't my double bubble sort algorithm sort; it just spits the
    >: numbers out, int the exact same order that I inputted them.
    >:
    >: Code:
    >:
    >: void swap(int& i,int& j)
    >: {
    >: i^=j;
    >: j^=i;
    >: i^=j;
    >: }
    >Unnecessarily complex,


    And incorrect, if passed two references to the same object.


    --
    Richard Herring
    Richard Herring, Apr 5, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Lyons

    Two-Dimensional Bubble Sort

    Scott Lyons, May 3, 2004, in forum: C++
    Replies:
    1
    Views:
    376
    Scott Lyons
    May 3, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,431
    Victor Bazarov
    Feb 17, 2005
  3. bubble sort

    , Aug 28, 2006, in forum: C++
    Replies:
    3
    Views:
    493
  4. Selection sort and bubble sort

    , Oct 16, 2007, in forum: C Programming
    Replies:
    22
    Views:
    1,723
    user923005
    Oct 19, 2007
  5. mlimber
    Replies:
    0
    Views:
    778
    mlimber
    Aug 5, 2008
Loading...

Share This Page