Quick sorting Algorithm idea: Quick sort (Quicksort) is an improvement on bubble sort. Quick Sort by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence. Using an algorithm to sort the image represents a quick sort of fast sorting algorithm is: 1) Set two variables I, J, when the Order begins: I=0,j=n-1;2) with the first array element as the key data, assigned to key, that is, key=a[0];3) from J to forward search, That is, after starting to search forward (j--), find the first value less than key A[j], will a[j] and A[i] interchange; 4) backward search from I, that is, from the beginning to search backward (i++), find the first greater than the key A[i], will a[i] and A[j] interchange, 5) repeat 3rd, 4 steps, Until I=j, (3,4 step, did not find the qualifying value, that is, 3 a[j] is not less than key,4 a[i] is not larger than the time of the key change J, I value, so j=j-1,i=i+1 until found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be precisely when the i+ or J-complete, at this time the end of the cycle) the most popular way:
The first number is compared with the other numbers in the array, and the small one is on the left and the other is on the right, so it's always recursively sorted.
The basic procedure for sorting is to use the array {49,38,65,97,76,13,27,49} as an example, selecting the first element 49 as the Datum
initialization keyword: [49,38,65,97,76,13,27,49] Code implementation:
1 Public Static voidMain (string[] args) {2 //TODO auto-generated Method Stub3 intA[] = {49, 38, 65, 97, 76, 13, 27, 49};//set an array4Sort (a,0,a.length-1);//Call the Quick Sort method5System.out.println (Arrays.tostring (a));//array output that will be sorted quickly6 }7 Public Static voidSortintA[],intLeftintRight ) {8 intI, J, key;9 if(Left > right) {//It's important to know that this is the only thing that jumps out when the function is recursive.Ten return; One } Ai = left;//start on the left first -j = right;//The last one on the right -key = A[i];//use the first digit of the array as the reference number the while(I < J) {//alternately scan from both ends of an array - while(I < J && A[j] >= key) {//start scanning from the right -j--; - } + //jumping out of the loop indicates that the number of the point on the right is smaller than the reference number, so two numbers swap seats, small to the left -A[i] =A[j]; + while(I<j && a[i]< key) {//and scan from the left . Ai++; at } - //the same as above, just the opposite. -A[J] =A[i]; - } -A[i] = key;//This is the loop to the last time a[j] = a[i]; Duplicate, I position number should be key, so replace back -Sort (A, left, i-1);//recursive ordering on less than key side inSort (A, i + 1, right);//recursive ordering above key side - //until both sides are sorted, jump out of this method in the if judgment above to quickly sort to}
Java Quick Sort algorithm