Skip to content

Add a recursive merge sort algorithm that accepts an array as input. #4462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 30, 2021
43 changes: 43 additions & 0 deletions sorts/recursive_mergesort_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
def merge(arr: list[int]) -> list[int]:
"""Return a sorted array.
>>> merge([10,9,8,7,6,5,4,3,2,1])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> merge([1,2,3,4,5,6,7,8,9,10])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> merge([10,22,1,2,3,9,15,23])
[1, 2, 3, 9, 10, 15, 22, 23]
"""
if len(arr) > 1:
middle_length = len(arr) // 2
left_array = arr[:middle_length]
right_array = arr[middle_length:]
left_size = len(left_array)
right_size = len(right_array)
merge(left_array)
merge(right_array)
i = 0
j = 0
k = 0
while i < left_size and j < right_size:
if left_array[i] < right_array[j]:
arr[k] = left_array[i]
i = i + 1
else:
arr[k] = right_array[j]
j = j + 1
k = k + 1
while i < left_size:
arr[k] = left_array[i]
i = i + 1
k = k + 1
while j < right_size:
arr[k] = right_array[j]
j = j + 1
k = k + 1
return arr


if __name__ == "__main__":
import doctest

doctest.testmod()