def parttion(v, left, right): key = v[left] low = left high = right while low < high: while (low < high) and (v[high] >= key): high -= 1 v[low] = v[high] while (low < high) and (v[low] <= key): low += 1 v[high] = v[low] v[low] = key return low def quicksort(v, left, right): if left < right: p = parttion(v, left, right) quicksort(v, left, p-1) quicksort(v, p+1, right) return v s = [6, 8, 1, 4, 3, 9, 5, 4, 11, 2, 2, 15, 6] print("before sort:",s) s1 = quicksort(s, left = 0, right = len(s) - 1) print("after sort:",s1)
before sort: [6, 8, 1, 4, 3, 9, 5, 4, 11, 2, 2, 15, 6] after sort: [1, 2, 2, 3, 4, 4, 5, 6, 6, 8, 9, 11, 15]
以上就是Python的快速排序方法的详细内容,更多请关注Gxl网其它相关文章!
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did84636