1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| class Solution: def quickSort(self,nums,low,high): pivot = nums[low] left,right=low,high while left<right:
while left<right and nums[right]>=pivot: right -= 1 nums[left] = nums[right]
while left<right and nums[left]<=pivot: left += 1 nums[right] = nums[left] nums[left] = pivot
return left def random_init(self,nums,low,high): pivot_idx = random.randint(low,high) nums[low],nums[pivot_idx] = nums[pivot_idx],nums[low]
return self.quickSort(nums,low,high)
def quick_sort(self,nums,low,high): if low >= high: return mid = self.random_init(nums,low,high)
self.quick_sort(nums,low,mid-1) self.quick_sort(nums,mid+1,high)
def sortArray(self, num1: List[int]) -> List[int]:
self.quick_sort(num1,0,len(num1)-1)
return num1
|