快速排序 和 分而治之(D&C)divde and conquer D&C 将问题逐步分解,找到最基本的情况为止 快速排序,随机的选择基准值,平均运行时间为**O(n*logn)** def quicksort(array): if len(array) < 2:# base case, arrays with 0 or 1 element are already "sorted" return array else:# recursive case pivot = array[0]# sub-array of all the elements less than the pivot less = [i for i in array[1:] if i <= pivot]# sub-array of all the elements greater than the pivot greater = [i for i in array[1:] if i > pivot] return quicksort(less) + [pivot] + quicksort(greater) print(quicksort([10, 5, 2, 3])) ## [2, 3, 5, 10]

Continue reading

Author's picture

Jixing Liu

Reading And Writing

Data Scientist

China