The original selection sort code
int [] arr = new int[]{3,2,6,9,34,66,11};
public static void Xuanze (int[] arr)
{
for (int x=0;x<arr.length-1;x++)
{
for (int y=x+1;y<arr.length;y++)
{
if (Arr[x]>arr[y]
{Arr[x]=arr[x]^arr[y];
Arr[y]arr[x]^arr[y];
Arr[x]=arr[x]^arr[y];
}
}
}
}
Analysis: Every time to meet the conditions are to do the data transposition, system resource consumption is relatively large
After optimization
public static void Xuanze_1 (int[] arr)
{
for (int x=0;x<arr.length-1; x + +) //Finish Extraction of X-bit elements
{
int temp =arr[x];
int index = x;
for (int y=x+1;y <arr.length; y++) //complete extraction of values for all elements outside X-bit
{
if (Temp>arr[y]) // Each time a condition is met, the data transposition can first record the value of the final comparison with a variable, and then make a line break
{
temp = arr[y];
index =y;
/* arr[x]=arr[x]^arr[y];//Complete interchange
Arr[y]=arr[x]^arr[y];
Arr[x]=arr[x]^arr[y]; */
}
}
if (X!=index)
{
arr[x]=arr[x]^arr[index];//Complete Interchange
Arr[index]=arr[x]^arr[index];
Arr[x]=arr[x]^arr[index];
}
}
}
The difference between data substitution and XOR or data substitution using third-party variables (a lot of video teaching doesn't talk about it)
The former can complete the permutation of all the data including two equal numbers
You can only replace two different numbers, otherwise the operation will be faulted. If you want to use this method to complete, you must add the first condition judgment. if (x!=y)
The former joins this judgment can reduce two equal data permutation, does not join also does not have the error, only then wasted the unnecessary system resources expense. While the latter wastes system resources, it also calculates errors. So if the judgment must be added.
Summary: No matter how we use that method, we have to add if judgment. The optimization of the bubbling method is not repeated.
A farmer's learning feelings: learning this programming, must personally knock code, must be repeated thinking, repeated knocking. That is a lot of video lessons in the teacher said, must do their own hands, repeated hands. Feel yourself know, will, and then prove to yourself, do know, indeed will. Extended theory-Practice-theory-practice, do anything to be fundamental to it. Do things conscientiously, unharmed do things. Do not want to sand to build a plateau, or at least not strong, and may fall halfway down, naught. Fix the heart to be able to fit the heart to the achievement. (Perhaps this sentiment only applies to the mediocre of my intellect)
2015-5-26
The seventh day of the JAVA faltering self-study array selection bubble sort optimization