fork download
  1. import random
  2. import sys
  3. sys.setrecursionlimit(1000000)
  4. class Solution(object):
  5. def sortArray(self, nums):
  6. self.quickSort(nums, 0, len(nums) - 1)
  7. return nums
  8.  
  9. def quickSort(self, nums, low, high):
  10. if low >= high:
  11. return
  12.  
  13. pivotIndex = random.randint(low, high)
  14. self.swap(nums, pivotIndex, high)
  15. pivot = nums[high]
  16.  
  17.  
  18. lt = low
  19. i = low
  20. gt = high
  21. while i <= gt:
  22. if nums[i] < pivot:
  23. self.swap(nums, lt, i)
  24. lt += 1
  25. i += 1
  26. elif nums[i] > pivot:
  27. self.swap(nums, i, gt)
  28. gt -= 1
  29. else:
  30. i += 1
  31.  
  32. self.quickSort(nums, low, lt - 1)
  33. self.quickSort(nums, gt + 1, high)
  34.  
  35. def swap(self, nums, i, j):
  36. nums[i], nums[j] = nums[j], nums[i]
  37.  
Success #stdin #stdout 0.13s 14216KB
stdin
Standard input is empty
stdout
Standard output is empty