- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathmerge_sort.py
25 lines (19 loc) · 784 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
defmerge_sort(values):
iflen(values) <=1:
returnvalues
else:
middle_index=len(values) //2
left_half=merge_sort(values[:middle_index])
right_half=merge_sort(values[middle_index:])
sorted_values= []
left_index=0; right_index=0
whileleft_index<len(left_half) andright_index<len(right_half):
ifleft_half[left_index] <=right_half[right_index]:
sorted_values.append(left_half[left_index])
left_index+=1
else:
sorted_values.append(right_half[right_index])
right_index+=1
sorted_values+=left_half[left_index:]
sorted_values+=right_half[right_index:]
returnsorted_values