Double Bubble Sort Algorithm Fails to Sort AT ALL

P

Protoman

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!!!!!!!!
 
A

Alf P. Steinbach

* 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.
 
L

loufoque

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

Heinz Ozwirk

Protoman said:
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
 
P

Protoman

loufoque said:
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++?
 
J

Jack Klein

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

Ivan Vecerina

: 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
 
R

Richard Herring

Ivan Vecerina said:
: 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top