Quick sort: a kind of improvement to bubble sort, if the initial record sequence is ordered or basically ordered by the keyword, degenerate to bubble sort. The recursive principle is used, and the average performance is the best among all orders of the same order of magnitude O (n longn) . In terms of average time, is currently considered the best of an internal sorting algorithm;
Basic idea: By a lying sort will be sorted into two separate parts of the data, 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.
Three pointers: The first pointer is called the PivotKey Pointer (pivot), the second pointer and the third pointer are both the left and right pointers, pointing to the leftmost and rightmost values, respectively. Left pointer and right pointer from both sides at the same time to the middle, in the process of approaching the pivot, will be smaller than the pivot of the elements moved to the low side, will be larger than the pivot of the elements moved to the high-end, the pivot is selected to never change, the final in the middle, before the small size;
The code is as follows:
1 Public classQuickSort {2 Public Static voidMain (string[] args) {3 int[] Array = {1, 2, 4, 6, 3, 0, 5 }; 4QuickSort (array, 0, array.length-1); 5 for(inti = 0; i < Array.Length; i++) { 6System.out.print (Array[i] + "--"); 7 } 8 } 9 Ten /** One * First write the algorithm according to the array for the data prototype, and then write the extensibility algorithm. Array {49,38,65,97,76,13,27} A * Recursive function - * @paramArray - * @param Left the * @param Right - */ - Public Static voidQuickSort (int[] Array,intLeftintRight ) { - intPivotKey; + if(Left <Right ) { -PivotKey =partitionbypivotvalue (array, left, right); + //recursive calls to left and right arrays are quickly sorted until the order is completely correct AQuickSort (array, left, pivotKey-1); atQuickSort (Array, PivotKey + 1, right); - } - } - - /** - * Pivotvalue as pivot, compared to small elements sorted in its left, compared to large elements after sorting in its right in * Split function - * @paramArray to * @param Left + * @param Right - * @return the */ * Public Static intPartitionbypivotvalue (int[] Array,intLeftintRight ) { $ intPivotvalue =Array[left]; Panax Notoginseng //Pivot is selected and will never change, eventually in the middle, before the small rear big - while(Left <Right ) { the while(Left < right && Array[right] >=pivotvalue) { +--Right ; A } the //move the element smaller than the pivot to the lower end, at which point the right bit is equal to NULL, waiting for the lower-than-pivotkey number to fill up +Array[left] =Array[right]; - while(Left < right && Array[left] <=pivotvalue) { $++Left ; $ } - //move the element larger than the pivot to the high end, at which point the left bit is equal to empty, waiting for a higher number to be smaller than the PivotKey . -Array[right] =Array[left]; the } - //when left = = right, complete a quick sequence, the left bit is equivalent to empty, waiting for PivotKey to fill upWuyiArray[left] =Pivotvalue; the returnLeft ; - } Wu}
Java Quick Sort