Double Bubble Sorting Problem!

i wanted to sort an array with double bubble sort but it didnt work!!!
please help me!
my code is this:

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--)
std::swap(i,j);
}
}
void main()
{
srand(time(0));
time_t start,end;
float dif;

int a[100]={0}; // array to sort
for(int i = 0 ; i < 100 ; i++) //filing the array
a[i] = (rand());

time (&start);
doubleBubbleSort(a,100);
printElements(a,100); // print elements
time (&end);
dif = difftime (end,start);
printf ("It took %.0f seconds to double bubble sort the array.\n", dif );

getch();
}

i will be glad full if any one tell me how to get time like 1.56 seconds,too!
1
2
if(array[i]>array[i++])
for(j=i;j>=0&&(array[j]>array[j++]);j--)


These lines both cause undefined behavior for the same reason that i = i++; causes undefined behavior.
what can i write in place?
Here's a straightforward translation of your (obviously still erroneous) code without undefined behavior:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void doubleBubbleSort(int *array, int length)
{
    int i,j;

    for(i=0; i<length--;i++)
    {
        if ( array[i] > array[i+1] )
        {
            for ( j=++i; j>=0 && array[j] > array[j+1]; j-- )
                std::swap(i,++j) ;
        }
        else
            ++i ;
    }
}


I don't know what a "double bubble sort" is, but I'm fairly certain that's not it.

Topic archived. No new replies allowed.