1) by a trip sorting the data to be sorted into separate two parts, one part of all the data is smaller than the other part of all the data, and then this method to the two parts of the data are quickly sorted, the entire sorting process can be recursively
Example:
Package Com.neuedu.algorithm;public class QuickSort {//Quick sort public static void main (String [] args) {int [] array= {323,6,6 6,78,23,567,-1}; System.out.println ("Before sorting:"); for (int i:array) {System.out.print (i+ " ");} QuickSort (array, 0, array.length-1);} private static void QuickSort (int[] arr, int _left, int _right) { int left=_left; int right=_right; int temp = 0;if (left <= right) { //the element to be sorted has at least two cases temp=arr[left];while (left!=right) {while (right>left& &ARR[RIGHT]>=TEMP)//Find the first number on the right that is less than the base right--;arr[left]=arr[right];while (Right>left&&arr[left] <=TEMP)//Find the first number on the left that is greater than the base left++;arr[right]=arr[left]; Arr[right] = temp; Datum element homing QuickSort (arr,_left,left-1); Recursively sorts the elements to the left of the Datum element QuickSort (arr, right+1,_right); Recursively sort the base element to the right of }}}
Analysis
The time of fast sorting is mainly spent on the division operation, the interval of length k is divided, the total need k-1 the comparison of the key words.
The worst-case scenario is that each time the selected datum is the smallest (or largest) record of the keyword in the current unordered region, the result is that the sub-interval to the left of the Datum is empty (or the right sub-interval is empty), and the number of records in another non-empty sub-interval is divided by only one less than the number of records Time Complexity of O (n*n)
In the best case, the datum for each partition is the "median" record of the current unordered region, and the result is that the length of the two unordered sub-ranges of the datum is roughly equal to the left and right. Total number of keyword comparisons: O (NLGN)
Although the worst time for fast sorting is O (N2), in terms of average performance, it is the fastest in the internal sorting algorithm based on the keyword comparison, and hence the name of the fast sort. Its average time complexity is O (NLGN).
Java implementation of fast sorting algorithm