Skip to content

Instantly share code, notes, and snippets.

@racterub
Created December 24, 2018 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save racterub/a510d64ecba699ab5ae465cc2600878e to your computer and use it in GitHub Desktop.
Save racterub/a510d64ecba699ab5ae465cc2600878e to your computer and use it in GitHub Desktop.
def Merge_Sort(array):
if len(array) > 1:
mid = len(array) // 2
left_array = array[:mid]
right_array = array[mid:]
Merge_Sort(left_array)
Merge_Sort(right_array)
right_index = 0;
left_index = 0;
merged_index = 0;
while right_index < len(right_array) and left_index < len(left_array):
if(right_array[right_index] < left_array[left_index]):
array[merged_index] = right_array[right_index]
right_index = right_index + 1
else:
array[merged_index] = left_array[left_index]
left_index = left_index + 1
merged_index = merged_index + 1
while right_index < len(right_array):
array[merged_index] = right_array[right_index]
right_index = right_index + 1
merged_index = merged_index + 1
while left_index < len(left_array):
array[merged_index] = left_array[left_index]
left_index = left_index + 1
merged_index = merged_index + 1
Numbers = [41, 33, 17, 80, 61, 5, 55]
Merge_Sort(Numbers)
print(Numbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment