swap integer problem

This is a discussion on swap integer problem within the C Programming forums, part of the General Programming Boards category; today i practised selection sorting algorithm with following Code: #include <stdio.h> #include <stdlib.h> #define LEN 10 void swap(int *a,int *b) ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    swap integer problem

    today i practised selection sorting algorithm with following

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define LEN 10
    
    void swap(int *a,int *b)
    {
    
            /*
            int temp = *a;
            *a = *b;
            *b = temp;
    
            */
            *a = *a ^ *b;
            *b = *a ^ *b;
            *a = *a ^ *b;
    }
    
    
    
    
    void selection_sort(int *arr, int len)
    {
            int i = 0;
           for(;i<len-1;i++)
            {
                    int j=i+1,min=i;
                    for(;j<len;j++)
                    {
                            if(arr[j] < arr[min])
                            {
                                    min = j;
                            }
                    }
                    swap(&arr[min],&arr[i]);
            }
    }
    
    int main()
    {
            int *a = malloc(sizeof(int) * LEN);
            int i = 0;
    
            for(;i<LEN;i++)
            {
                    a[i] = rand()%300;
                    printf("%d\t",a[i]);
            }
            printf("\n");
    
            selection_sort(a,LEN);
    
            for(i=0;i<LEN;i++)
            {
                    printf("%d\t",a[i]);
            }
            printf("\n");
            free(a);
    }

    if i use code block
    int temp = *a;
    *a = *b;
    *b = temp;

    csxia-laptop:/data/learning/C/DataStructure # ./selection_insert
    283 286 177 115 293 235 286 192 249 121
    115 121 177 192 235 249 283 286 286 293


    it works fine , but if i use the latter code block
    *a = *a ^ *b;
    *b = *a ^ *b;
    *a = *a ^ *b;
    then a and b's value will get zero if the address of a and b is the same

    csxia-laptop:/data/learning/C/DataStructure # ./selection_insert
    283 286 177 115 293 235 286 192 249 121
    115 121 0 192 235 249 283 0 286 293


    i have no idea why, anyone who can help me???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,733
    Quote Originally Posted by csxia
    it works fine , but if i use the latter code block
    *a = *a ^ *b;
    *b = *a ^ *b;
    *a = *a ^ *b;
    then a and b's value will get zero if the address of a and b is the same

    i have no idea why, anyone who can help me???
    Think about what happens when you write:
    Code:
    int x = 2;
    x = x ^ x;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    Quote Originally Posted by csxia View Post
    but if i use the latter code block
    *a = *a ^ *b;
    *b = *a ^ *b;
    *a = *a ^ *b;
    then a and b's value will get zero if the address of a and b is the same
    i have no idea why, anyone who can help me???
    It's because XOR of equal numbers is 0.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    OMG this is a bug

    laserlight, thanks for your quick reply

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    It is not a bug, that is how XOR works. The problem is in using an annoying XOR swap when it is a bad idea. Stick with your first swap code. Sure, it has a temp, so? It is clear, and straightforward.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  4. A homework problem about C++ (Pointer)
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2005, 03:28 PM
  5. problem with random integer
    By techno logic in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2002, 02:20 PM

Tags for this Thread


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