Fast sorting is a very classic, efficient, and commonly used sorting algorithm. Many of the sorting algorithms in the standard library of languages are useful to it.
Principle
The fast-running principle is actually relatively simple, which is to solve the problem by splitting the original large array into decimal groups.
You have to find a place to dismantle it. If this position is called the pivot point, then the quick sort problem becomes the constant to find the position of the split Fulcrum element.
It is often found that the fulcrum is based on an element, from the rightmost element to the left to find a position smaller than the specified element, and then from the left to the right to find a position larger than the specified element. If the two positions are not the same, swap two positions, and continue to look for the table from both ends. Finding the right place is the fulcrum we need. The elements on either side of the fulcrum repeat the above operation until only one element is left in the split sub-array. The split is over and the order is taken.
So the question is, which element is the standard to compare? For example, you can choose the first element.
Complexity of
Ideally, the fulcrum can be found to split the array into sub-arrays of similar length and left, with the time Complexity O (N*LOGN)
The worst-case scenario is that each time a fulcrum element is found, the other side is completely wasted, and the process of finding a fulcrum is wasted. This time will reach O (n^2).
Because it disrupts the original order of the same elements, the fast line is also an unstable sort. So it is commonly used in the sorting of ordinary types of data.
Code implementation
Package Mainimport ("Time" "FMT" "Math/rand") func main () {var length = ten var list []int//with timestamp for seed generation with Number of machines to ensure that each run of the data does not repeat r: = Rand. New (Rand. Newsource (time. Now (). Unixnano ()))) for I: = 0; i < length; i++ {list = append (list, int (R.INTN))} FMT. PRINTLN (list) quickSort (list, 0, length-1) fmt. PRINTLN (list)}func quickSort (list []int, start, end int) {////Only one element left) returns if Start >= end {return} Mark leftmost element as reference tmp: = List[start]//Two cursors move from opposite ends to find the appropriate "pivot" Left: = Start Right: = End for Left!! {//the cursor on the right moves to the left until it finds a smaller value than the referenced element for List[right] >= tmp && Go < right {right-- }//The left cursor moves to the right until it finds a value larger than the reference element values for List[left] <= tmp && LH < OK {left++ }//If the found two cursor position is not uniform, the value of the cursor position element, and continue the next round to find//At this time the exchange of the left and right position of the value, must not be greater than the left side. may be equal but will also exchange positions, so it is called an unstable sorting algorithm if left < right {List[left], list[right] = List[rIght], List[left] FMT. PRINTLN (List)}}//At this time the left position is the fulcrum we are looking for, swap position List[start], list[left] = List[left], tmp//press the Fulcrum position bar the original sequence number is divided into Two paragraphs, then each gradually narrow the range sort QuickSort (list, start, left-1) QuickSort (list, left+1, end)}
Run results
Zatan
In the worst case scenario, the performance of the above algorithm is worse, similar to the normal insertion sort. So in order to avoid choosing a bad pivot point, you can choose the middle element of the array as the criterion of comparison, or choose 3 elements to take the intermediate size elements as reference items. Performance can be optimized to some extent. However, the fastest scenario is too rare to be ignored.
There is also an optimization point, that is, when the amount of data is very small, the fast line does not reflect the speed advantage, but in more than 20 elements within the sort is slower than the insertion sort. So you can add a judgment in the recursive loop, if the number of elements on one side is less than a certain value (such as 20), insert sort is used directly.
[Golang] Data structure-quick sort