Algorithmic Thinking: Quick sort uses the idea of divide and conquer, that is, select a datum in the selected array (either one can), base on the base of the reference, move the elements less than that datum to the left of the datum, move the data that is larger than the datum to the right, and then recursively process the left and right sides. Also according to the above method, that is: Select the benchmark, in recursion. Algorithm example: arr=[10,5,2,3,4,7,6]---> [2, 3, 4, 5, 6, 7, 10] The code is as follows:
Several of the above code can be simplified as follows:
Greater,less = [],[]
For i in Arr[1:]: Lite version less = [I-I in arr[1:] if I <= pivot]
If I <= pivot: ==========>>>>> greater = [I for I in arr[1:] if i > Pivot]
Less.append (i)
Else
Greater.append (i)
Algorithm Performance Analysis:
In the worst case, the time complexity is O (n^2)
On average, the time complexity is O (NLOGN)
There are two key points in the quick sort:
First, the judgment of the recursive exit
For recursive exits, we can consider several special cases:
When there are no elements in the array, it should be returned directly, and should be returned directly when there is only one element in the array. Therefore, when an element in an array is empty or has only one element, the program should return.
Second, the determination of recursive expressions
For a quick sort, consider it from its algorithmic thinking, which should be the case.
[Less than Datum] + [datum] + [greater than Datum]
After we have made this clear, we then call the quick sort on the less-than-datum and larger-than-datum parts. This allows us to get a fast-line recursive expression.
Quicksort (Less_part) + [benchmark] + quicksort (great_part)
The middle process of fast sequencing is actually a recursive tree. When the recursion reaches the leaf node, then the recursion ends, and the program executes.
Fast sequencing of Python algorithms