leetcode 912

Posted on 2020-10-23 21:16  黑炽  阅读(53)  评论(0编辑  收藏  举报
 1 class Solution:
 2     def sortArray(self, nums: List[int]) -> List[int]:
 3         self.quickSort(nums, 0, len(nums)-1)
 4         return nums
 5 
 6     def quickSort(self, nums, left, right):
 7         left = 0 if not isinstance(left, int) else left
 8         right = len(nums)-1 if not isinstance(right, int) else right
 9         if left < right:
10             partitionIndex = self.partition(nums, left, right)
11             self.quickSort(nums, left, partitionIndex-1)
12             self.quickSort(nums, partitionIndex+1, right)
13         return nums
14     
15     def partition(self, nums, left, right):
16         pivot = left
17         index = pivot+1
18         i = index
19         while i <= right:
20             if nums[i] < nums[pivot]:
21                 self.swap(nums, i, index)
22                 index+=1
23             i+=1
24         self.swap(nums, pivot, index-1)
25         return index-1
26 
27     def swap(self, nums, i, j):
28         nums[i], nums[j] = nums[j], nums[i]