Velocity Reviews > C++ > Double Bubble Sort Algorithm Fails to Sort AT ALL

# Double Bubble Sort Algorithm Fails to Sort AT ALL

Protoman
Guest
Posts: n/a

 04-02-2006
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[i]>array[i++])
for(j=i;j>=0&&(array[j]>array[j++]);j--)
swap(array[j],array[j++]);
}
}

Any help? Thanks!!!!!!!!

Alf P. Steinbach
Guest
Posts: n/a

 04-02-2006
* 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[i]>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?

loufoque
Guest
Posts: n/a

 04-02-2006
Protoman wrote :

> void doubleBubbleSort(int *array, int length)
> {
> int i,j;
> for(i=0; i<length--;i++)
> {
> if(array[i]>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.

Heinz Ozwirk
Guest
Posts: n/a

 04-02-2006
"Protoman" <(E-Mail Removed)> schrieb im Newsbeitrag news:(E-Mail Removed) ups.com...

> if(array[i]>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

Protoman
Guest
Posts: n/a

 04-02-2006

loufoque wrote:
> Protoman wrote :
>
> > void doubleBubbleSort(int *array, int length)
> > {
> > int i,j;
> > for(i=0; i<length--;i++)
> > {
> > if(array[i]>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++?

Jack Klein
Guest
Posts: n/a

 04-02-2006
On Sun, 02 Apr 2006 12:17:04 +0200, loufoque
<(E-Mail Removed)> wrote in comp.lang.c++:

> Protoman wrote :
>
> > void doubleBubbleSort(int *array, int length)
> > {
> > int i,j;
> > for(i=0; i<length--;i++)
> > {
> > if(array[i]>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/~a...FAQ-acllc.html

Ivan Vecerina
Guest
Posts: n/a

 04-03-2006
"Protoman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
: 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[i]>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.
This aside, when you write " array[i]>array[i++] ",
which side of the expression do you expect to refer
to array[i+1] ??

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

loufoque
Guest
Posts: n/a

 04-03-2006
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.

Richard Herring
Guest
Posts: n/a

 04-05-2006
In message <b52a1\$4430e886\$3e028af2\$(E-Mail Removed)>, Ivan Vecerina
<(E-Mail Removed)> writes
>"Protoman" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed) oups.com...
>: 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