QuickSort is an optimization algorithm for Bubble sorting.
Basic Idea: sort the records to be sorted into two separate parts. the keywords of some records are smaller than those of other records, then, the two records can be sorted to achieve the purpose of sorting the entire sequence.
Pivot keyword): select a keyword in the column to be sorted, and try to place it in a position so that the value on the left is smaller than it, and the value on the right is larger than it, we will make this keyword pivot ).
Generally, the keyword at the leftmost end of the sorting is pivot.
# Include <stdio. h> # include <stdlib. h> void swap (int arr [], int I, int j) {int temp; temp = arr [I]; arr [I] = arr [j]; arr [j] = temp;} // divides the array to be sorted into two parts. The value on the left side of the pivot is smaller than the pivot value, and the value on the right side is greater than the pivot value, the Return Value of the function contains int Partition (int arr [], int low, int high) {int lower tkey; // The pivot value pivotkey = arr [low]; // take the first value to be sorted as the pivot value while (low
Quick Sort Complexity Analysis
Optimal Condition: Each split into two parts is very uniform, that is, the data of the two parts is similar. Only recursive logn times are required. The algorithm time complexity is O (nlogn)
Worst case: to be sorted in positive or reverse order, each split is empty on one side. n-1 recursive calls are required, and the I-th Division must be compared by n-I, so the number of comparisons is n-1 + N-2 +... + 1 = n (n-1)/2. The time complexity is O (n2 ).
Average:
Complexity: O (nlogn );
Fast sorting is unstable.
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1282083