# Double Bubble Sort Algorithm Fails to Sort AT ALL

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

1. ### ProtomanGuest

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

2. ### Alf P. SteinbachGuest

* 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

3. ### loufoqueGuest

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
4. ### Heinz OzwirkGuest

"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
5. ### ProtomanGuest

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
6. ### Jack KleinGuest

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
7. ### Ivan VecerinaGuest

"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.
This aside, when you write " array>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
Ivan Vecerina, Apr 3, 2006
8. ### loufoqueGuest

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
9. ### Richard HerringGuest

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