Before the text
Quick Sort (English: Quicksort), also known as split-exchange sequencing (Partition-exchange sort), is a sort algorithm that was first proposed by Tony Hall. In the average situation, sort n items to o(nlogn) comparisons, in the worst case an o(n2) comparison is required, but this is not a common situation. In fact, fast sequencing is often significantly faster than other O(nlogn) algorithms because its internal loop (inner Loop) can be implemented efficiently on most architectures.?????????????? --wikipedia
This article describes the following:
Sorting principle
Algorithm implementation (JAVA)
Testing phase
Algorithm analysis
Principle of body sorting
and merge Sort by:
Merge sort: Divides an array into two word groups, sorts two parts separately, merges the ordered sub-arrays to get the entire ordered array
Quick sort: Divides an array into two word groups, sorting two parts separately, and when two sub-arrays are ordered, the entire array is naturally ordered.
Quick sort uses a split element to divide the array, the element to the left of the Shard element is smaller than the Shard element, and the right array element is larger than the Shard element, so two sub-arrays are ordered to get the entire ordered array
Algorithm implementation 1. Cut Fraction Group
private static int partition(int[] a, int low, int high){ int i = low, j = high + 1; //由两边向中间扫描 int p = a[low]; //切分数组的元素 while(true){ while(a[++i] < p){ // i 的值会一直递增至 a[++i] >= p if(i == high){ //扫描结束 break; } } while(a[--j] > p){ // j 的值会一直递减至 a[++i] >= p if(j == low){ //扫描结束 break; } } if(i >= j){ // j 的位置就是切分元素在有序数组中的位置 break; } int temp = a[i]; //交换元素顺序 a[i] = a[j]; a[j] = temp; } int temp = a[low]; //将切分元素交换至中间位置 a[low] = a[j]; a[j] = temp; return j; }
2. Array sorting
This part of the code is similar to the sort code for merge sort
private static void quickSort(int[] a){ sort(a, 0, a.length - 1); } private static void sort(int[] a, int lo, int hi){ if(hi <= lo){ return ; } int j = partition(a, lo, hi); sort(a, lo, j - 1); sort(a, j + 1, hi); }
The sort method recursively calls itself, dividing the array to a minimum, and each time the first element in the subsequence is used as the Shard element to sort the array
Code for the entire sorting process:
public class QuickSort {private static int partition (int[] A, int. low, int.) {int i = low, j = high + 1; Scan int p = A[low] from both sides to the middle; The elements of the tangent fraction group while (true) {while (A[++i] < P) {//I values are incremented to a[++i] >= p if (i = = high) {//scan end break; }} while (A[--j] > P) {//J values will always be decremented to A[++i] >= p if (j = = Low) { Scan end break; }} if (I >= j) the position of {//J} is the position of the Shard element in the ordered array break; } int temp = A[i]; Interchange element order A[i] = a[j]; A[J] = temp; } int temp = A[low]; Swap The Shard element to the middle position a[low] = a[j]; A[J] = temp; Return J; } private static void QuickSort (int[] a) {sorT (A, 0, a.length-1); } private static void sort (int[] A, int lo, int hi) {if (Hi <= lo) {return; } int J = partition (A, lo, HI); Sort (A, lo, j-1); Sort (A, j + 1, HI); }}
Testing phase
public static void main(String[] args){ int[] a = new int[10]; for (int i = 0; i < 10; i++) { a[i] = (int)(Math.random() * 100); System.out.print(a[i] + " "); } quickSort(a); System.out.println(); for (int i = 0; i < 10; i++) { System.out.print(a[i] + " "); } }
Generate 10 random numbers and call the quick Sort method, and the result is the final output:
Algorithm Analysis 1. Characteristics
Quick sort implementation is simple, widely used, much faster than the general algorithm, and is in-situ sequencing (with the help of the stack)
2. Complexity of Time
The length of the array is n, and t (n) indicates the number of comparisons required to sort an array of length n, as can be imagined, T (0) and T (1) 0
The cost of splitting is N + 1, the average cost for the left sub-array sort is (C1 + C2 + ... + CN-2 + CN-1)/n, the average cost for sorting right sub-arrays is (CN-1 + CN-2 + ... + C2 + CN)/n
CN = N + 1 + (C1 + C2 + ... + CN-2 + CN-1)/n + (CN-1 + CN-2 + ... + C2 + CN)/n
After simplification get: CN ~ 2 (n + 1) (1/3 + + + ... + 1/(n + 1))
Points get CN ~ 2Nlogn
Therefore, the algorithm complexity is O (nlogn)
3. Stability
For example, an array a = [6,4,3,2,3,7,8,7,9],a[0] and a[4] exchanges, the second 3 moves to the front of the first 3 , so is unstable, instability is reflected in the split element exchange time
The introduction to the quick Sort is here, thank you!
"Algorithm" sort (v) Quick Sort