Merge Sort
def merge_sort(nums):
# bottom cases: empty or list of a single element.
if len(nums) <= 1:
return nums
pivot = int(len(nums) / 2)
left_list = merge_sort(nums[0:pivot])
right_list = merge_sort(nums[pivot:])
return merge(left_list, right_list)
def merge(left_list, right_list):
left_cursor = right_cursor = 0
ret = []
while left_cursor < len(left_list) and right_cursor < len(right_list):
if left_list[left_cursor] < right_list[right_cursor]:
ret.append(left_list[left_cursor])
left_cursor += 1
else:
ret.append(right_list[right_cursor])
right_cursor += 1
# append what is remained in either of the lists
ret.extend(left_list[left_cursor:])
ret.extend(right_list[right_cursor:])
return ret
Quick sort
def quicksort(lst):
"""
Sorts an array in the ascending order in O(n log n) time
:param nums: a list of numbers
:return: the sorted list
"""
n = len(lst)
qsort(lst, 0, n - 1)
def qsort(lst, lo, hi):
"""
Helper
:param lst: the list to sort
:param lo: the index of the first element in the list
:param hi: the index of the last element in the list
:return: the sorted list
"""
if lo < hi:
p = partition(lst, lo, hi)
qsort(lst, lo, p - 1)
qsort(lst, p + 1, hi)
def partition(lst, lo, hi):
"""
Picks the last element hi as a pivot
and returns the index of pivot value in the sorted array
"""
pivot = lst[hi]
i = lo
for j in range(lo, hi):
if lst[j] < pivot:
lst[i], lst[j] = lst[j], lst[i]
i += 1
lst[i], lst[hi] = lst[hi], lst[i]
return i