Quick sorting: it is also called partition exchange sorting. Find a benchmark in the sequence, and divide the data to be sorted into two independent parts by one-time sorting. All the data in one part is smaller than all the data in the other part, and then quickly sort the two parts of data by this method, and complete the sorting of all the data by analogy.
Optimal time complexity: O(nlogn)
Worst time complexity: O(n ²)
It's an unstable sort
""" //Quick sorting: find a benchmark in the sequence, and divide the data to be sorted into two independent parts by one sorting, one of which has more data than the other //All the data in the part should be small, and then the two parts of the data are sorted rapidly by this method, and so on to complete the sorting of all the data. """ def quickSort(start,end,dataList): """ :param start: Start of sort in sequence :param end: End of sort in sequence :param dataList: Sort the sequence in ascending order by default :return: """ #Recursive exit. When start=end indicates that there is only one element in the subsequence, and it has been placed in the correct position without sorting. if start>=end: return # Take the first element of the sequence as the benchmark by default flag = dataList[start] #Left to right cursor in sequence low=start #Right to left cursor in sequence high = end #In ascending order, move elements larger than the flag to the right and elements smaller than the flag to the left. #Be sure to consider the same elements as the flag, or there will be a dead cycle. while low<high: #Start from the right to the left. If the value is larger than the flag, continue to the left, end-1. #When an element smaller than a flag is encountered, move the data to the position of data[low] while low<high: if dataList[high] > flag: high-=1 else: #When they are equal, the data will also be moved to the left of the flag, so fast sorting is not stable. dataList[low]=dataList[high] low+=1 #After you jump out of the inner loop, you will start looking left to right break #From left to right while low<high: if dataList[low]<=flag: low+=1 else: dataList[high] = dataList[low] high-=1 break #Jump out of the above while loop, indicating that at this time low=flag, you can determine the subscript of the flag in the sequence. dataList[low]=flag quickSort(start,low-1,dataList) quickSort(low+1,end,dataList) dataList = [12,32,1,43,54,23,32,12,99,2] quickSort(0,9,dataList) print(dataList)