快速排序算法

快速排序算法

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)
posted @ 2022-05-09 02:21  楚千羽  阅读(25)  评论(0编辑  收藏  举报