merge:
class Solution(object):
def sortArray(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if len(nums) < 2:
return nums
mid = len(nums) // 2
left = self.sortArray(nums[: mid])
right = self.sortArray(nums[mid: ])
return self.merge_sort(left, right)
def merge_sort(self, left, right):
startleft = 0
startright = 0
res = []
while startleft < len(left) and startright < len(right):
if left[startleft] < right[startright]:
res.append(left[startleft])
startleft += 1
else:
res.append(right[startright])
startright += 1
res += left[startleft:]
res += right[startright:]
return res
quick sort:
def quicksort(self, nums):
if len(nums) < 2:
return nums
base = nums[0]
less = [i for i in nums[1:] if i < base]
more = [i for i in nums[1:] if i >= base]
return self.quicksort(less) + [base] + self.quicksort(more)