python merge sort

Run Settings
LanguagePython
Language Version
Run Command
def merge(array_1 , array_2): temp = [] while (array_1 and array_2): if array_1[0] > array_2[0]: temp.append(array_2[0]) array_2 = array_2[1:] else: temp.append(array_1[0]) array_1 = array_1[1:] if array_1: temp = temp + array_1 elif array_2: temp = temp + array_2 return temp def merge_sort(nums): if not nums: raise ValueError("") if len(nums) == 1: return nums mid = len(nums) // 2 slice_1 = nums[0:mid] slice_2 = nums[mid:] array_1 = merge_sort(slice_1) array_2 = merge_sort(slice_2) return merge(array_1, array_2) print(merge_sort([3,1,2,6]))
Editor Settings
Theme
Key bindings
Full width
Lines