"Basic Ideas"
Fast ordering in the case of more elements, the sorting efficiency is quite high. The basic idea is this:
Suppose the array is int[] arr = {49, 38, 65, 97, 76, 13, 27, 22, 26, 41, 13, 17, 32}, and the number of array elements is 13.
Select A[0] is the left marker, a[12] is the right marker, and the initial position of the Datum pivot is generally a[0] (its value is P).
The definition i,j represents the changing left and right markers, respectively.
At this point, let the datum point return to the position. That is, p is the datum, the left element is less than p, the right element is greater than p.
The specific is:
1. The left tag keeps moving to the right, comparing the size of P with the location of the element, if it is larger than p, stop comparing, note this point;
2. The right tag keeps moving to the left, comparing the size of P with the location of the element, if it is smaller than p, stop comparing, note this point;
3. Exchanging left and right marks;
4. Until the left and right markers overlap, this position is the position of the datum point.
The above steps are then repeated by the two small sequences separated by the datum points.
"Code Implementation"
Public Static voidQuickSort (int[] arr,intLeftintRight ) { //if left is not less than right, the part that needs to be sorted has only one element, and the method ends the call. if(Left >=Right ) { return; } //set the leftmost element to pivot (datum point) intp =Arr[left]; //Put the smaller than p on the left side, bigger than p to the right. inti = left, j =Right ; while(I <j) {//J Move left to find an element that is smaller than p. while(Arr[j] >= p && i <j) {J--; } //I move to the right to find an element larger than p. while(Arr[i] <= p && i <j) {I++; } //I and J Exchange. if(I <j) {inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; } } //let the datum point return to the position, at this time I and J are the same, with anyone. Arr[left] =Arr[i]; Arr[i]=p; //the elements of the sequence on the left of the datum point are recursively ordered quickly. QuickSort (arr, left, i-1); //the elements of the sequence on the right of the datum point are recursively ordered quickly. QuickSort (arr, i + 1, right); }
Common sort methods in Java-quick sort (ascending)