---restore content starts---
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 this method to the two parts of the data are quickly sorted, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence
Basic Steps
Three-digit take in
In the process of fast-running, each time we take an element as the pivot value, this number is used to divide the sequence into two parts. Here we take the three-digit method, that is, take the left, middle, and right end three numbers, and then to sort, the middle number as the pivot value.
Split based on pivot values
Code implementation
Package Sortdemo;import java.util.arrays;/** * Created by Chengxiao on 2016/12/14. * Quick Sort */public class QuickSort {public static void main (string[] args) {int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; QuickSort (arr, 0, arr.length-1); System.out.println ("Sort result:" + arrays.tostring (arr)); }/** * @param arr * @param left hand pointer * @param right pointer */public static void QuickSort (int[] arr, int left, int right) {if (left < right) {//Gets the pivot value and places it at the end of the current pending sequence Dealpivot (arr, leave, rig HT); The pivot value is placed at the end of the sequence int pivot = right-1; Left pointer int i = IEFT; Right pointer int j = right-1; while (true) {while (Arr[++i] < Arr[pivot]) {} while (J > left && Amp ARR[--J] > Arr[pivot]) {} if (I < j) {Swap (arr, I, j); } else { Break }} if (I < right) {swap (arr, I, right-1); } quickSort (arr, left, i-1); QuickSort (arr, i + 1, right); }}/** * Processing hub value * * @param arr * @param left * @param right */public static void Dealpiv OT (int[] arr, int left, int. right) {int mid = (left + right)/2; if (Arr[left] > Arr[mid]) {swap (arr, left, mid); } if (Arr[left] > Arr[right]) {swap (arr, left, right); } if (Arr[right] < Arr[mid]) {swap (arr, right, mid); } swap (arr, right-1, mid); }/** * Swap element General processing * * @param arr * @param A * @param b * * private static void swap (int[] arr, int a, int b) {int temp = Arr[a]; Arr[a] = arr[b]; ARR[B] = temp; }}
Sort results
Sorted results: [1, 2, 3, 4, 5, 6, 7, 8]
Summarize
Fast sorting is a sort of exchange class, and it is also a classical embodiment of the method of partition. Splits the sequence to be sorted into two groups in a single trip, with some of the recorded keywords less than the other. The two groups are then sequentially sorted to make the entire sequence orderly. In the process of segmentation, the selection of pivot value is very important, this paper adopts three-bit method, which can avoid the case of grouping "one-sided" to a great extent. The average time complexity of the quick sort is also O (Nlogn) level.
---restore content ends---
The method of three-digit-taking in fast sorting