For this question, I personally think it is necessary to practice the partition function. After all, it is the core of fast sorting, a basic thing, and must be mastered, as for the clever sexual solution given in the book, it is an investigation of personal thinking ability. It is a way of thinking, not just a question or two.
The partition function, including the quick sorting function, must be handy and necessary.
Import randomdef morethanhalf (array): If Len (array) = 0: Return 0 start = 0end = Len (array)-1 m = end> 1 Index = partition (array, start, end) while index! = M: If index> M: Index = partition (array, start, index-1) Elif index <m: Index = partition (array, index + 1, end) return array [Index] def partition (array, start, end): If start <= end: Return startindex = random. randint (START, end) I = startj = endwhile I <= J: If array [I] <= array [Index]: I + = 1 Elif array [J]> = array [Index]: J + = 1 else: array [I], array [J] = array [J], array [I] return I