From 650039a279f0eef4f2e267d27c6b2d5be2e18fa0 Mon Sep 17 00:00:00 2001 From: Benjamin Fein Date: Sun, 30 May 2021 11:27:42 -0400 Subject: [PATCH] Add a recursive merge sort algorithm that accepts an array as input. (#4462) This is a different recursive implementation of the merge sort algorithm. * Recursive Merge Sort That Accepts an Array Recursive Merge Sort That Accepts an Array * Add Wikipedia Link * Fixes naming conventions * Update sorts/recursive_mergesort_array.py Co-authored-by: Maxim R. <49735721+mrmaxguns@users.noreply.github.com> * Update sorts/recursive_mergesort_array.py Co-authored-by: Maxim R. <49735721+mrmaxguns@users.noreply.github.com> * Update sorts/recursive_mergesort_array.py Co-authored-by: Maxim R. <49735721+mrmaxguns@users.noreply.github.com> * Update sorts/recursive_mergesort_array.py Co-authored-by: Maxim R. <49735721+mrmaxguns@users.noreply.github.com> * Update sorts/recursive_mergesort_array.py Co-authored-by: Maxim R. <49735721+mrmaxguns@users.noreply.github.com> * Adds black format * Removes unused variables * Fixes variable names and adds documentation * Fixes variable names to use snake_case. * Removes double #. * Update sorts/recursive_mergesort_array.py Co-authored-by: Maxim R. <49735721+mrmaxguns@users.noreply.github.com> Co-authored-by: Maxim R. <49735721+mrmaxguns@users.noreply.github.com> Co-authored-by: Benjamin Fein --- sorts/recursive_mergesort_array.py | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 sorts/recursive_mergesort_array.py diff --git a/sorts/recursive_mergesort_array.py b/sorts/recursive_mergesort_array.py new file mode 100644 index 000000000..f714d0238 --- /dev/null +++ b/sorts/recursive_mergesort_array.py @@ -0,0 +1,64 @@ +"""A merge sort which accepts an array as input and recursively +splits an array in half and sorts and combines them. +""" + +"""https://en.wikipedia.org/wiki/Merge_sort """ + + +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] + >>> merge([100]) + [100] + >>> merge([]) + [] + """ + if len(arr) > 1: + middle_length = len(arr) // 2 # Finds the middle of the array + left_array = arr[ + :middle_length + ] # Creates an array of the elements in the first half. + right_array = arr[ + middle_length: + ] # Creates an array of the elements in the second half. + left_size = len(left_array) + right_size = len(right_array) + merge(left_array) # Starts sorting the left. + merge(right_array) # Starts sorting the right + left_index = 0 # Left Counter + right_index = 0 # Right Counter + index = 0 # Position Counter + while ( + left_index < left_size and right_index < right_size + ): # Runs until the lowers size of the left and right are sorted. + if left_array[left_index] < right_array[right_index]: + arr[index] = left_array[left_index] + left_index = left_index + 1 + else: + arr[index] = right_array[right_index] + right_index = right_index + 1 + index = index + 1 + while ( + left_index < left_size + ): # Adds the left over elements in the left half of the array + arr[index] = left_array[left_index] + left_index = left_index + 1 + index = index + 1 + while ( + right_index < right_size + ): # Adds the left over elements in the right half of the array + arr[index] = right_array[right_index] + right_index = right_index + 1 + index = index + 1 + return arr + + +if __name__ == "__main__": + import doctest + + doctest.testmod()