Quick sorting
Fast sorting also uses a divide-and-conquer policy to improve performance without additional memory. However, the cost of doing so is that the list is not segmented in half, so the performance will decrease.
Select a value for quick sorting, which is generally called "axis point". Although there are many ways to select axis points, we simply use the first element in the list as the axis point. The role of the axis point is to help divide the List into two parts. After the list is complete, the axis point is located at the "split point". The list is divided into two parts for subsequent calls.
As shown in figure 12, 54 is used as the axis point. We have already arranged this example many times. We know that 54 will be in the position of 31 after sorting. After finding the axis point, divide other elements in the List into two parts by means of greater or smaller than the axis point.
Figure 12 first axis point of quick sorting
Splitting starts with locating two location signs ...... It's called "left Mark" and "right mark ...... It is located at the beginning and end of the remaining list elements (1 and 8 digits in figure 13 ). The goal of splitting is to move the element with the wrong position to the correct position relative to the axis point, and the left and right labels will also get together on the "split point.
Examples/examples + INCh09ogPGNvZGU + bGVmdG1hcms8L2NvZGU + examples/nT0MfQt9a149fzsd + examples/Examples "http://www.2cto.com/uploadfile/Collfiles/20140504/20140504090159242.jpg" alt = "\">
Figure 14 Complete splitting
Below is the implementationquickSort. ItA recursive function is called.quickSortHelper. quickSortHelperThe base point of. is the same as the merge sort. If the length of the list is smaller than or equal to 1, it is ordered. If it is greater than 1, it will be split and recursively sorted.PartitionThe function completes the splitting process mentioned above.
def quickSort(alist): quickSortHelper(alist,0,len(alist)-1)def quickSortHelper(alist,first,last): if first
= pivotvalue and \ rightmark >= leftmark: rightmark = rightmark -1 if rightmark < leftmark: done = True else: temp = alist[leftmark] alist[leftmark] = alist[rightmark] alist[rightmark] = temp temp = alist[first] alist[first] = alist[rightmark] alist[rightmark] = temp return rightmarkalist = [54,26,93,17,77,31,44,55,20]quickSort(alist)print(alist)
To analyze the performance of the quick sorting function, you must note that a list with a length of n is always in the middle of the list.NSplit. To locate the split point, n elements must be compared with the axis point.NLogNIn addition, it does not need to consume additional memory like merging and sorting. Unfortunately, in the best case, the splitting point is not in the middle, but is left or right to generate uneven sub-tables. In this case, the generated sub-table is a 0 element, one n-1 elements, and then split, but generate a sub-table is 0 elements, the other is N-2 elements and so on. The overhead of the result isO(N2 ).
As mentioned above, there are different ways to select the axis point. In particular, we can use a technology called "three-digit splitting" to reduce the possibility of uneven splitting. This method takes the median value from the first, middle, and last element in the list. In the preceding example, the three numbers are 54,77 and 20, and 54 is selected as the axis point. The source of this idea is that the size of the first element may not be in the middle, but the median value of the three numbers is close to the center. This method is especially suitable for the partially ordered list at the beginning. The selection method of this axis point value is reserved for exercise.