# Circular bubblesort

• 04-21-2006
PiCoMiKe
Circular bubblesort
Hello!!

Code:

```void bubblesort(int n, double *x) { int i, j;   for (i=1 ; i<=n-1 ; i++)                for (j=n-1 ; j>=i ; j--)       if (x[j-1] > x[j])          swapd(&x[j-1], &x[j]);                }```
I want to modify the upper bubblesort in order to have a circular behaviour.
I tried this but it doesn't seem to work right:
Code:

```void bubblesort(int n, double *x, int point) {   int i,j;   for(i=point+1 ; i!=point-1 ; i = (i+1)%n)       for(j=point-1 ; j!=i ;  j = ((j<=0) ? (n-1) : (j-1)))       if (x[((j<=0) ? (n-1) : (j-1))] > x[j])         swap(&x[((j<=0) ? (n-1) : (j-1))], &x[j]);```
In both cases swap is:
Code:

```void swapd(double *a, double *b)    { double temp;   temp = *a;   *a = *b;   *b = temp; }```
Variable point, is the point from where I want to start sorting my array, from the element x[point] till x[point-1].
As u can see when point is equal to zero the circular bubblesort should work as the typical one (1st code example).

BUT mine doesn't work :( ..
Does anybody know what is wrong with it?

• 04-21-2006
dwks
Just pass your array as &array[point];
• 04-21-2006
PiCoMiKe
Quote:

Originally Posted by dwks
Just pass your array as &array[point];

I can't figure out where it would help sth like that.
Can u be more specific?

I mean the 1st code example works great as a typical bubblesort, but the 2nd one that tries to behave circular doesn't!!

The problem appears to be in the for functions ;) !!