Skip to content

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions divide_and_conquer/mergesort.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ def merge(arr, left, mid, right):
return arr


def mergesort(arr, left, right):
def mergesort(arr, left=None, right=None):
"""
>>> mergesort([3, 2, 1], 0, 2)
>>> mergesort([3, 2, 1])
[1, 2, 3]
>>> mergesort([3, 2, 1, 0, 1, 2, 3, 5, 4], 0, 8)
>>> 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 is None or left is False or left < 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if left is None or left is False or left < 0:
if (left or -1) < 0:

left = 0
if right is None or right is False or right < 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if right is None or right is False or right < 0:
if (right or -1) < 0:

right = len(arr) - 1
if left < right:
mid = (left + right) // 2
# print("ms1",a,b,m)
Expand Down