Home > Back-end >  Use different way or exchange of two variables in the Java trap in the array
Use different way or exchange of two variables in the Java trap in the array

Time:10-03

Exchange of two variables, there are many kinds of methods, not introduce one by one.
Most people can think of first method is to define a temporary variable,
As follows:
Int a=5;
int b=10;

int temp=a;
a=b;
b=temp;
The result is a=10, b=5;
Although this approach using a temporary variable, maybe a little weak on the performance a little (not reach significant level), but data exchange is safe and readability is very high also, it is recommended to use this method.

However, some like to reflect their is technology the great spirit of the students, like to use different or exchanged, as follows:

int a=10;
int b=20;

A=a ^ b;
B=a ^ b;
A=a ^ b;
At this time the result of a=20; B=10;
This method USES the characteristics of the xor, itself does not have what problem, because the operation is the binary operations, also does not define a new variable, in theory, performance will be a few taller,
And, of course, also can hold a great god, and show their technology, it is also flawed, however, readability is poor.

Write here, the method of exclusive or if it's just readability is poor, so what also don't need to say that it has a trap. But is it true?
The trap here from the selection is required.
As we all know, selection of the underlying implementation is as follows:

Public static void the sort (int [] arr) {
For (int I=1; I & lt;=arr. Length - 1; I++) {
Int minindex=I - 1;
For (int j=I; J & lt;=arr. Length - 1; J + +) {
If (arr [minindex] & gt; Arr [j]) {
Minindex=j;
}
}
Int temp=arr [I - 1);
Arr=arr [I - 1] [minindex];
Arr [minindex]=temp;
}
}

Accustomed to using different or to exchange feel taken for granted can write like this:
Public static void the sort (int [] arr) {
For (int I=1; I & lt;=arr. Length - 1; I++) {
Int minindex=I - 1;
For (int j=I; J & lt;=arr. Length - 1; J + +) {
If (arr [minindex] & gt; Arr [j]) {
Minindex=j;
}
}
Arr=arr [I - 1]] [I - 1 ^ arr [minindex];
Arr [minindex]=arr] [I - 1 ^ arr [minindex];
Arr=arr [I - 1]] [I - 1 ^ arr [minindex];
}
}

If you think so, so congratulations you into the trap!
Input array
Int [] arr=,4,4,7,8,9,5,4,6 {1};
Results output unexpectedly become like this:
[0, 0, 0, 4, 5, 6, 7, 0, 9]
What???

Analysis results will know the reason, not because use xor methods cannot be exchanged, but because of who I am index of an array of problems!
Selection principle is to assume that the first elements to the smallest one element, and then it has to do with the element after the comparison, less than the elements of this element,
Record the index position of the smallest element, then the current smallest element to continue with the element after the comparison, until completion of the round of comparison,
Then get the minimum values and assumptions of the minimum position.

So that elements with possible hypothesis is the actual minimum elements, so in a more down, a
Minindex=I - 1, not minindex=j;

In minindex=I - 1 case,
If I=1 to simplify is:
Arr [0]=arr [0] ^ arr [0].
Arr [0]=arr [0] ^ arr [0].
Arr [0]=arr [0] ^ arr [0].
, see here, you should understand the index is 0 elements turned out to be the exclusive or itself, and we know that the variable or itself, the result is zero!
So using exclusive or write selection sort, is there any change you need to determine the current index, namely:
If (minindex!=I - 1) {
Arr=arr [I - 1]] [I - 1 ^ arr [minindex];
Arr [minindex]=arr] [I - 1 ^ arr [minindex];
Arr=arr [I - 1]] [I - 1 ^ arr [minindex];
}
Analysis of all the way here, I think you can also see the trap.

Summary:
With great spirit, use different or the exchange of two variables is risky, mastered technology didn't fall off!
So I recommend to use the first method, simple and clear, good readability, which are beneficial to man.

CodePudding user response:

CodePudding user response:

Strong,,,

CodePudding user response:

Experts, to learn

CodePudding user response:

Experience, start a face of meng,

CodePudding user response:

I am writing the quick sort overturned, also wonder why is 0
  • Related