-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMerge_Sort.py
28 lines (21 loc) · 873 Bytes
/
Merge_Sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def sortMerge(nums):
#Our base case, if the list is 1 item long we can return itself because it's already sorted
if len(nums) == 1: return nums
#Recursively merge sort left and right lists
left = sortMerge(nums[:len(nums) // 2])
right = sortMerge(nums[len(nums) // 2:])
sortedNums, i, j = [], 0, 0
#While i and j are less than he lengths of each of their lists
while i < len(left) and j < len(right):
#Add the smaller item in the left list or right list and increment our counter
if left[i] < right[j]:
sortedNums += [left[i]]; i += 1
else:
sortedNums += [right[j]]; j += 1
#Add the rest of the numbers as we've exhausted one of the lists
sortedNums += left[i:] + right[j:]
return sortedNums
def main():
nums = [6, 7, 3, 2, 9, 3, 1, 0]
print(sortMerge(nums))
main()