-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ad4b522
Recursive Merge Sort That Accepts an Array
benfein efa34bd
Add Wikipedia Link
benfein d022cfa
Fixes naming conventions
benfein 7023837
Update sorts/recursive_mergesort_array.py
benfein c20610e
Update sorts/recursive_mergesort_array.py
benfein 13c9042
Update sorts/recursive_mergesort_array.py
benfein 999e144
Update sorts/recursive_mergesort_array.py
benfein 97e9423
Update sorts/recursive_mergesort_array.py
benfein d92f687
Adds black format
benfein 94a9323
Removes unused variables
benfein 751e9b8
Fixes variable names and adds documentation
f087db3
Fixes variable names to use snake_case.
5a0c7ec
Removes double #.
6d978ab
Update sorts/recursive_mergesort_array.py
benfein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.