Quick sort Just remember two steps:
1. Locate a datum point, place the number greater than the datum point to the right of the Datum point, and the number less than the datum point to the left of the Datum point (can also be reversed, less than the right side, and greater than the left side)
2. The array obtained in step 1 is divided by the datum point to two parts (the left side is less than the number of datum points, the right side is greater than the number of datum points), respectively, the two parts recursively call step 1
The implementation code is as follows:
ImportJava.io.*; Public classQuickSort { Public Static voidMain (string[] args) {int[] arr = {9, 2, 2, 10, 3, 27, 1};//Array to sortQuickSort (arr, 0, Arr.length-1); for(inti = 0; i < arr.length; i++) {System.out.println (Arr[i]+ " "); } } /*arr: Array to sort left: Sort start position right: sort Stop position*/ Private Static voidQuickSort (int[] arr,intLeftintRight ) { if(Left > Right | | arr = =NULL|| Arr.length = = 0) { return; } intPivot = Arr[left];//base on the value of the left boundary inti =Left ; intj = right;//using local variables to receive parameters while(I! = j) {//When i = J indicates that it is sorted (that is, the datum point to the left is less than the datum point and the right side of the datum point is greater than the datum point) while(Arr[j] >= pivot && i < J) {//find the number less than the datum point from right to leftj--; } while(Arr[i] <= pivot && i < J) {//from left to right, find a number greater than the Datum pointi++; } if(I < J) {//look for the above/below the reference point of the subscript has not met (met on the end of the round to sort out the loop), this time swap two positions intTMP =Arr[i]; Arr[i]=Arr[j]; ARR[J]=tmp; } } //return the base number to the positionArr[left] =Arr[i]; Arr[i]=pivot; QuickSort (arr, left, I-1); QuickSort (arr, I+1, right); }}
Java Quick Sort