Thread: Circular bubblesort

  1. #1
    Registered User PiCoMiKe's Avatar
    Join Date
    Apr 2006
    Location
    Greece, Athens
    Posts
    2

    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?

    Thanks in advance!!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Just pass your array as &array[point];
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User PiCoMiKe's Avatar
    Join Date
    Apr 2006
    Location
    Greece, Athens
    Posts
    2
    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 !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circular Positioning in C# form application?
    By arcaine01 in forum C# Programming
    Replies: 4
    Last Post: 05-08-2008, 02:31 PM
  2. Circular Lists
    By dudeomanodude in forum C++ Programming
    Replies: 65
    Last Post: 03-05-2008, 09:26 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. circular shift
    By n00bcodezor in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 03:51 AM
  5. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM