Last active
July 28, 2020 04:56
Python Merge Sort
This file contains hidden or 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) -> List: | |
if len(array) < 2: | |
return array | |
middle = floor(len(array)/2) | |
left = merge_sort(array[:middle]) | |
right = merge_sort(array[middle:]) | |
sorted_array = [] | |
while len(left) and len(right): | |
if left[0] < right[0]: | |
sorted_array.append(left.pop(0)) | |
else: | |
sorted_array.append(right.pop(0)) | |
return sorted_array + left + right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment