Bubble Sort

This is a discussion on Bubble Sort within the C Programming forums, part of the General Programming Boards category; Code: a ^= b ^= a ^= b is the same as a ^= b b ^= a a ^= ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    44

    Bubble Sort

    Code:
    a ^= b ^= a ^= b 
    
    is the same as 
    
    a ^= b 
    b ^= a 
    a ^= b 
    
    which can be written as 
    
    a = a xor b 
    b = b xor a = b xor (a xor b) = a 
    a = a xor b = (a xor b) xor a = b
    would it be acceptible to use this in a bubble sort.?
    Dangerous Dave

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    No

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    44
    Why???
    Dangerous Dave

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Because it is undefined, Nutshell started a thread about this earlier and several reasons were given for why it shouldn't be used.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest
    Here is a function you can use. I call it 'stringxor'. It is used for swapping strings:
    Code:
    void stringxor( char *s1, char *s2 )
    {
        int x,y= strlen(s1)>strlen(s2)?strlen(s1):strlen(s2);
        for(x=0;x<y;x++) s1[x]^=s2[x]^=s1[x]^=s2[x];
        printf(":D");
    }
    Enjoy.

    Quzah.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What the hell happened to my sign in? Freeking cookies.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    44
    Code:
    for(i=0; i <= SIZE - 1 ; i++)
       for(j =0; j <= SIZE - 1 ; j++)
       {
          while(strcmp(book_array[i].name, book_array[j].name) < 0)
          {
          strcpy(hold, book_array[j].name);
          strcpy(book_array[j].name, book_array[i].name);
          strcpy(book_array[i].name, hold);
          }
    I think i will stick with this for swapping the strings.
    Dangerous Dave

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I think i will stick with this for swapping the strings.

    That's a good idea, since mine was intended as a joke that will likely crash your application.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Now quzah, you know better than to write code that doesn't self document. This is better:
    Code:
    char *stringxor( char *s1, char *s2 )
    {
        int x,y= strlen(s1)>strlen(s2)?strlen(s1):strlen(s2);
        for(x=0;x<y;x++) s1[x]^=s2[x]^=s1[x]^=s2[x];
        return "If you use this you are stupid :)";
    }
    -Prelude
    My best code is written with the delete key.

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Dangerous Dave
    Code:
    for(i=0; i <= SIZE - 1 ; i++)
       for(j =0; j <= SIZE - 1 ; j++)
       {
          while(strcmp(book_array[i].name, book_array[j].name) < 0)
          {
          strcpy(hold, book_array[j].name);
          strcpy(book_array[j].name, book_array[i].name);
          strcpy(book_array[i].name, hold);
          }
    I think i will stick with this for swapping the strings.
    1) Why are you using 'while'? It's just executing once. 'if' would be more obvious.

    2) To make that sorting even more optimized, start on 'i' instead of '0' in the second loop.
    Code:
    for(i=0; i < SIZE - 1; i++)
       for(j=i; j < SIZE; j++)
       {
          if(strcmp(book_array[i].name, book_array[j].name) < 0)
          {
             strcpy(hold, book_array[j].name);
             strcpy(book_array[j].name, book_array[i].name);
             strcpy(book_array[i].name, hold);
          }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  2. My bubble sort only sorts once
    By Muller in forum C Programming
    Replies: 8
    Last Post: 03-27-2009, 04:36 PM
  3. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 03:54 PM
  5. Help with Bi-Directional Bubble Sort in C
    By cunninglinguist in forum C Programming
    Replies: 0
    Last Post: 04-19-2002, 02:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21