Fast sorting is fast, probably thinking is: First use a numeric value as the median, by first sorting the array into two parts, the left is smaller than this value, the right is larger than this value, and then use recursion to the two parts of the array to the same sort:
1DefQuick_sort (Li, Start, end):2#Recursive end Condition:3If Start >=End4Return5#First Index on left6 left =Start7#The last index on the right8 right =End9#Take the first number as an intermediate valueTen mid =Li[left]11#First, the right-hand index moves to the left, and the loop is executed when the left<right is greater than the mid value.12While left <Right13While left < rightand Li[right] >=MidRight-= 1Li[left] =Li[right]16While left < rightand Li[left] <=Mid-Left + = 1Li[right] =Li[left]Li[left] =Mid20#At this point, the number to the left of mid is smaller than mid, and the number to the right of mid is greater than mid21 # The numbers on both sides are sorted recursively. Span style= "COLOR: #008080" >22 Quick_sort (Li, start, Left-123 Quick_sort (Li, left + 1, end) 24 25 26 if __name__ = = " __main__ ' :27 li = [4, 3, 5, 7 , 9, 2, 1, 6, 8]28 Quick_sort (li, 0, Len (LI)-1) 29 print (LI)
George Bernard Shaw said: "You have an apple, I have an apple, exchanged with each other, we still each have an apple; but you have a thought, I have a kind of thinking, exchange each other, only to find that from different angles to see the problem far more profound than itself, so much communication is a way to improve our self-ability!!!
If you have a better and simpler way, please be sure to comment thank you!!!
Python for quick sorting