Quick sorting analysis: for the operation of array subscript, it is necessary to pay attention to the initial value setting of conditional loop variable and the judgment of loop end condition, and pay attention to the multivariable control of conditional structure. Sometimes it's very convoluted and requires more proficiency, so more training is needed. In order to make the loop effective and avoid the array out of bounds and other problems.
The operation effect (method body) of quick sorting is: 1. Divide the incoming array into three parts: the first part is a sub array (part of the incoming array), the second part is a number, and the third part is a sub array. This number is called the middle number. Since this is called the middle number, you can definitely understand the left side of the middle number Edge array means that all array elements in the first part are less than or equal to the middle number, and all array elements in the second part are greater than or equal to the middle number. 2. Then let the first part and the third part of the molecular array call the method itself as the new incoming array, that is, use recursion. The sign of the end of recursion is that when the incoming array is a single element array (the array contains only one element, of course, there is no need to sort), it stops, so that all the original array elements have been sorted.
My personal code implementation, notes:
public class QuickSort {
public static void quickSort(int[] array,int startIndex,int endIndex) {//Prepare incoming array, first element subscript, last element subscript
if(startIndex<endIndex) {//Single element arrays stop because no sorting is required
int LR=startIndex+1,RL=endIndex;//Two variables that record the subscript of an array element
while(true) {
for(int i=startIndex+1;i<=endIndex;i++) {//Record the first subscript greater than or equal to the first element from left to right
if(array[i]>=array[startIndex]) {
LR=i;
break;
}
LR++;
}
for(int j=endIndex;j>=startIndex+1;j--) {//Record the first subscript less than or equal to the first element from right to left
if(array[j]<=array[startIndex]) {
RL=j;
break;
}
RL--;
}
if(LR<RL) {//If the subscript value of the record is greater than or equal to the first element is less than or equal to the subscript value of the record, then the two subscripts represent the exchange of element values, and then loop
int temp=array[LR];
array[LR]=array[RL];
array[RL]=temp;
}else {
break;
}
}
if(LR>=RL) {//If the subscript value of the record is greater than or equal to that of the first element, then the element value represented by the subscript of the record is exchanged with the value of the first element
int temp=array[RL];//At this time, the value of the first element becomes the middle number. The left side of the middle number is a sub array, and the right side is another sub array
array[RL]=array[startIndex];
array[startIndex]=temp;
quickSort(array,startIndex,RL-1);//The middle left subarray (represented by three incoming parameters) is used as the parameter to call the method itself, that is, recursion is used
quickSort(array,RL+1,endIndex);//The middle right subarray (represented by three incoming parameters) is used as the parameter to call the method itself, that is, recursion is used
}
}
}
public static void main(String[] args) {
int[] array= {26,17,5,33,87,53,27,49,28,78};// TODO Auto-generated method stub
System.out.println("Before sorting:");
for(int i=0;i<array.length;i++)
System.out.print(array[i]+" ");
System.out.println();
quickSort(array,0,array.length-1);
System.out.println("After sorting:");
for(int i=0;i<array.length;i++)
System.out.print(array[i]+" ");
}
}
Operation result:
Before sorting:
26 17 5 33 87 53 27 49 28 78
After sorting:
5 17 26 27 28 33 49 53 78 87