快速排序算法
快速排序算法
import random
class Solution():
def quickSort(self, arr, head,tail):
if head >= tail:
return arr
mid = head + random.randint(0,tail - head)
pivot = arr[mid]
arr[mid] = arr[head]
low = head
high = tail
while low != high:
while low < high and arr[high] >= pivot:
high -= 1
arr[low] = arr[high]
while low < high and arr[low] <= pivot:
low += 1
arr[high] = arr[low]
arr[low] = pivot
Solution.quickSort(self, arr, head, low - 1)
Solution.quickSort(self, arr, low + 1, tail)
return arr
if __name__ == '__main__':
s = Solution()
arr = list(map(int,input().strip().split()))
kk = s.quickSort(arr,0,len(arr)-1)
print(kk)
作者:楚千羽
出处:https://www.cnblogs.com/chuqianyu/
本文来自博客园,本文作者:楚千羽,转载请注明原文链接:https://www.cnblogs.com/chuqianyu/p/16247758.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利!