BUG: Swap two elements when encountering a pit!

Source: Internet
Author: User

There are three different ways of exchanging elements:

    Stupid and multi-space Intermediate method
     temp = A;
     A = b;
     b = temp;

   XOR
   a = a^b;
   b = a^b;
   A = A^b;

   Phase subtraction, the principle is actually different or
   a = a+b;
   b = a-b;
   A = A-b;

I found that when I was writing a selection, I realized that when I used a different or commutative array element , there would be an unexpected situation ...
Select sort

Paste Normal Code

public static void Selectionsort (int [] arr) {
        int location = 0;
        int temp = 0;
        for (int i=0;i<arr.length-1;i++) {//Select to the penultimate position
            location = i;
            for (int j=i+1;j<arr.length;j++) {
                if (Arr[j]<arr[location]) {
                    location = j;
                }
            }
            temp = Arr[i];
            Arr[i] = arr[location];
            Arr[location] = temp;
        }
    }

Use array int [] arr = {4,3,2,1,5,6,8,7};

Get the results

1->2->3->4->5->6->7->8->

That's the right result.

But I just want to make the exchange of the place to do the optimization

            Arr[i] = arr[i]^arr[location];
            Arr[location] = arr[i]^arr[location];
            Arr[i] = arr[i]^arr[location];

The result should still remain unchanged.
But the magical thing came ...

The results have been

1->2->0->0->0->0->7->8->

... What's the situation? a trap of a different or

        int [] arr = {4,3,2,1,5,6,8,7};
        int a = 3;
        int b = 4;
        A = A^b;
        b = a^b;
        A = A^b;
        System.out.println ("a=" +a+ "; b=" +b);

        We exchange arr[0] and arr[0]
        arr[0] = arr[0]^arr[0];
        Arr[0] = arr[0]^arr[0];
        Arr[0] = arr[0]^arr[0];

        System.out.println (arr[0]);

The result is

a=4; B=3
0

Did you find the problem.

Or for exchanging elements, most of the time is fine. But when it comes to the exchange of array elements, you need to be cautious ...

If we exchange the same subscript element, it is equivalent to doing three different things to this element, and the final value is absolutely 0 .... how to avoid. for the exchange of array elements, try to take a conservative form of the intermediate amount. If you want to use a different or or phase subtraction, then you must first judge the subscript, if the subscript the same direct return. Element exchange for non-array conditions please use boldly

OK, let's go back and look at the previous code ...

First-round sort 1,3,2,4,5,6,8,7 no problem.
Second round sort 1,2,3,4,5,6,8,7 no problem

We'll look at the third round sort.

Because this time we start to choose the third position, that is, the correct element of the array subscript 2, we find that 3 is the smallest element in the position, that is, location and I are the same value , and the inner loop does not change any values.

So the next thing we're doing is

     ARR[2] = arr[2]^arr[2];
     ARR[2] = arr[2]^arr[2];
     ARR[2] = arr[2]^arr[2];

The result must be 0. remember .....

The method of intermediate quantity absolutely absolutely will not go wrong. so say. Bubble sort is good

 public static void bubble (int [] arr) {for (int i=0;i<arr.length-1;i++) {f
                    or (int j=0;j<arr.length-1-i;j++) {if (arr[j]>arr[j+1]) {///This place can be assured of bold use of XOR, because J and j+1 will never be equal
                    ARR[J+1] = arr[j]^arr[j+1];
                    ARR[J] = arr[j]^arr[j+1];
                ARR[J+1] = arr[j]^arr[j+1]; }
            }
        }
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.