defQuick_sort (alist, Start, end):"""Quick Sort""" #Recursive exit conditions ifStart >=End:return #sets the starting element as the datum element to find the positionMID =Alist[start]#Low is the left-to-right cursor for the left of the sequenceLow =Start#High is the right-to-left cursor to the right of the sequenceHigh =End whileLow <High :#If low is not coincident with high, the element with high points is not smaller than the datum element, then high moves to the left whileLow < High andAlist[high] >=Mid:high-= 1#Place the high- pointing element in the low positionAlist[low] =Alist[high]#If low is not coincident with high, the element that is pointed to is smaller than the datum element, and the lower is moved to the right whileLow < High andAlist[low] <Mid:low+ = 1#Place the element with low pointing to the high positionAlist[high] =Alist[low]#after exiting the loop, low is coincident with high, at which point the position is the correct position of the datum element #Place a datum element at that locationAlist[low] =Mid#Quick ordering of sub-sequences to the left of a datum elementQuick_sort (alist, start, low-1) #Quick ordering of sub-sequences to the right of a datum elementQuick_sort (Alist, low+1, end) Alist= [54,26,93,17,77,31,44,55,20]quick_sort (Alist,0,len (alist)-1)Print(alist)
- Optimal time complexity: O (NLOGN)
- Worst time complexity: O (n2)
- Stability: Unstable
- Pick an element from the sequence called "Datum" (pivot),
- Reorder the columns, where all elements are placed in front of the datum in a smaller position than the base value, and all elements are larger than the base value behind the datum (the same number can be on either side). At the end of this partition, the datum is in the middle of the sequence. This is called partition (partition) operation.
- recursively (recursive) sorts sub-columns that are smaller than the base value elements and sub-columns that are larger than the base value elements.
By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.
Python Quick Sort