Created
January 31, 2023 17:19
-
-
Save Aleksey-Danchin/769f449d4f328e2ca83d1528fbac9883 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge_sort(array: list): | |
length = len(array) | |
buff = [0 for _ in range(length)] | |
def merge_sort_master(start_index, finish_index): | |
size = finish_index - start_index + 1 | |
if size < 2: | |
return | |
size //= 2 | |
merge_sort_master(start_index, start_index + size - 1) | |
merge_sort_master(start_index + size, finish_index) | |
index = start_index | |
left_index = start_index | |
right_index = start_index + size | |
while left_index <= start_index + size - 1 and right_index <= finish_index: | |
if array[left_index] < array[right_index]: | |
buff[index] = array[left_index] | |
left_index += 1 | |
else: | |
buff[index] = array[right_index] | |
right_index += 1 | |
index += 1 | |
while left_index <= start_index + size - 1: | |
buff[index] = array[left_index] | |
left_index += 1 | |
index += 1 | |
while right_index <= finish_index: | |
buff[index] = array[right_index] | |
right_index += 1 | |
index += 1 | |
for i in range(start_index, finish_index+1): | |
array[i] = buff[i] | |
merge_sort_master(0, length - 1) | |
return array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment