Disclaimer: copyright. You are welcome to reprint it. Contact mailbox: yiluohuanghun@gmail.com]
This afternoon, I was not too busy at the company. I also summarized a sorting algorithm, which was a bit lazy, mainly for fast sorting. Quick sorting is actually the most commonly used sorting algorithm, just like the name, fast, and efficient. Fast sorting is based on Bubble sorting. In general, the worst case is that the benchmark selected by each division is the record with the smallest (or largest) keyword in the unordered area, the division result is that the subinterval on the left of the benchmark is null (or the subinterval on the right is empty), and the number of records in the subinterval obtained from the Division is not empty, only one fewer record count than the unordered partition before division. The time complexity is O (n * n). In the best case, the benchmark obtained by each division is the "medium value" Record of the unordered area, the division result is that the length of the left and right unordered subintervals of the benchmark is roughly equal. Total keyword comparison times: O (nlgn) although the worst time for fast sorting is O (n2), in terms of average performance, it is the fastest internal Sorting Algorithm Based on keyword comparison, and its name is also obtained. Its average time complexity is O (nlgn ).
Quick Sort idea:
Quick sorting is A sort by Division and exchange proposed by C. R. A. Hoare in 1962. It adopts a sub-Governance Policy, usually called Divide-and-ConquerMethod ).
1) Basic Idea of divide and conquer Law
The basic idea of the division and control law is to break down the original problem into several subproblems with smaller sizes but similar structures as the original problem. Recursively solve these subproblems, and then combine the solutions of these subproblems into the solutions of the original problem.
2) Basic Idea of fast sorting
Set the unordered area to R [low .. high], and describe the basic idea of fast sorting using the division and control method as follows:
① Decomposition:
In R [low .. select a record in high] as the benchmark to divide the current disordered zoning into two smaller subintervals (left and right) R [low .. pivotpos-1) and R [pivotpos + 1 .. high], and make the keywords of all records in the left subinterval less than or equal to the benchmark record (may be recorded as the benchmark) keyword. key, the key of all records in the subinterval on the right is greater than or equal to limit. the benchmark record is located at the correct position (pivotpos), and does not need to be sorted in the future.
Note:
The key to division is to determine the location of the benchmark record, TPOs. The division result can be simply expressed as (note that partition = R [pivotpos]):
R [low... pivotpos-1]. keys ≤ R [small TPOs]. key ≤ R [small TPOs + 1 .. high]. keys
Here, low ≤ TPOs ≤ high.
② Solution:
Use recursive call to quickly sort the Left and Right subintervals R [low... pivotpos-1] and R [small TPOs + 1 .. high.
③ Combination:
Because when the two recursive calls in the "solving" Step end, the left and right subintervals are ordered. For quick sorting, the "Combination" step does not need to be done, and can be considered as a null operation.
This section from http://student.zjzk.cn/course_ware/data_structure/web/paixu/paixu8.3.2.1.htm)
It may not be too intuitive to say this. Let's say it again in vernacular. Quick sorting is to find an element. In theory, you can find one as the benchmark. Then, you can partition the array so that the value of the element on the left of the benchmark is not greater than the benchmark value, the element values on the right of the benchmark are not smaller than the benchmark value, so that the elements used as the benchmark are adjusted to the correct position after sorting. Recursive quick sorting: adjust other n-1 elements to the correct position after sorting. Finally, each element is in the correct position after sorting, and the sorting is complete. Therefore, the core algorithm of the quick sorting algorithm is partitioning, that is, how to adjust the benchmark location and adjust the final location of the returned benchmark for segmentation and recursion.
For example, this may not be easy to understand. Assume that the sequence to be sorted is
2 2 2 4 9 3 6 7 1 5 first use 2 as the benchmark, use the I j pointer to scan the two sides respectively, and separate elements smaller than 2 and larger than 2. First, compare the values of 2 and 5 to 2, and move j left.
2 2 4 9 3 6 7 1 5 compare 2 and smaller than 2, so put 1 at the 2 position
2 1 4 9 3 6 7 1 5 compare 2 and 4 4 is greater than 2, so move 4 to the back
2 1 4 9 3 6 7 4 5 compare 2 and 7, 2 and 6, 2 and 3, 2 and 9, all greater than 2, meet the conditions, so unchanged
After the first round of fast sorting, the elements become the following
After [1] 2 [4 9 3 6 7 5], the elements on the left of 2 are arranged in a quick row. Because there is only one element, the quick row ends. Right side
Perform quick sorting and recursion to generate the final result.
#include <stdio.h>#include <stdlib.h>void quickSort(int array[], int start, int end){ if(start < end) { int key = array[start]; int low = start; int high = end; while(low < high) { while((low < high) && key < array[high]) high--; array[low] = array[high]; while((low < high) && key > array[low]) low++; array[high] = array[low]; } array[low] = key; quickSort(array, start, low - 1); quickSort(array, low + 1, end); }}int main(int argc, char *argv[]){ int index = 0; int array[] = {2, 4, 9, 3, 6, 7, 1, 5}; quickSort(array, 0, 7); for(; index < 8; index++) { printf("%d\t", array[index]); } system("pause"); return 0;}
This article from the "Yi fall Dusk" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/1266266