-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Better mergesort.py #2444
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
Better mergesort.py #2444
Conversation
Allow the function to be executed without having to pass the start and end indexes of the array
Added validation for negative or False values
@cclauss can you take a look why Travic CI build error. Thanks in advance. |
Build log says
There are two files named |
Reverting the name change will fix the issue. It is bugging me to have two different merge sorts in two different directories. Maybe we should just have one in the sorts directory. |
""" | ||
if left is None or left is False or left < 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if left is None or left is False or left < 0: | |
if (left or -1) < 0: |
""" | ||
if left is None or left is False or left < 0: | ||
left = 0 | ||
if right is None or right is False or right < 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if right is None or right is False or right < 0: | |
if (right or -1) < 0: |
@cclauss That conditional won't work, value will always be -1 Try running this: def mergesort(num, left=None):
yyy = (left or -1)
if (left or -1) < 0:
print('inside if', num, yyy)
mergesort(0)
mergesort(1, None)
mergesort(2, False)
mergesort(3, -1)
mergesort(4, 0)
mergesort(5, 1) And you will get: inside if 0 -1
inside if 1 -1
inside if 2 -1
inside if 3 -1
inside if 4 -1 |
>>> for i in range(-5, 5):
... left = i
... if (left or -1) < 0:
... left = 0
... print(i, left)
...
-5 0
-4 0
-3 0
-2 0
-1 0
0 0
1 1
2 2
3 3
4 4 |
Please run this, Executed on:
|
After adding your suggested changes I get Here is the file with your changes applied @cclauss def merge(arr, left, mid, right):
# overall array will divided into 2 array
# left_arr contains the left portion of array from left to mid
# right_arr contains the right portion of array from mid + 1 to right
left_arr = arr[left : mid + 1]
right_arr = arr[mid + 1 : right + 1]
k = left
i = 0
j = 0
while i < len(left_arr) and j < len(right_arr):
# change sign for Descending order
if left_arr[i] < right_arr[j]:
arr[k] = left_arr[i]
i += 1
else:
arr[k] = right_arr[j]
j += 1
k += 1
while i < len(left_arr):
arr[k] = left_arr[i]
i += 1
k += 1
while j < len(right_arr):
arr[k] = right_arr[j]
j += 1
k += 1
return arr
def mergesort(arr, left=None, right=None):
"""
>>> mergesort([3, 2, 1])
[1, 2, 3]
>>> mergesort([3, 2, 1, 0, 1, 2, 3, 5, 4])
[0, 1, 1, 2, 2, 3, 3, 4, 5]
>>> mergesort([3, 2, 1], -1, -3.1)
[1, 2, 3]
>>> mergesort([3, 2, 1], None, False)
[1, 2, 3]
"""
if (left or -1) < 0:
left = 0
if (right or -1) < 0:
right = len(arr) - 1
if left < right:
mid = (left + right) // 2
# print("ms1",a,b,m)
mergesort(arr, left, mid)
# print("ms2",a,m+1,e)
mergesort(arr, mid + 1, right)
# print("m",a,b,m,e)
merge(arr, left, mid, right)
return arr
if __name__ == "__main__":
import doctest
doctest.testmod() |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Please reopen this pull request once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to seek help from our Gitter or ping one of the reviewers. Thank you for your contributions! |
Allow the function to be executed without having to pass the start and end indexes of the array
Describe your change:
Added default values to
mergesort
function to automatically calculate the start and end indexesIncludes @cclauss revision
Checklist:
Fixes: #{$ISSUE_NO}
.