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

Better mergesort.py #2444

wants to merge 2 commits into from

Conversation

LuisEnMarroquin
Copy link

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 indexes

Includes @cclauss revision

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?
  • Improved an algorithm?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Allow the function to be executed without having to pass the start and end indexes of the array
@LuisEnMarroquin LuisEnMarroquin mentioned this pull request Sep 17, 2020
15 tasks
Added validation for negative or False values
@realDuYuanChao
Copy link
Member

@cclauss can you take a look why Travic CI build error. Thanks in advance.

@spamegg1
Copy link
Contributor

spamegg1 commented Sep 19, 2020

@cclauss can you take a look why Travic CI build error. Thanks in advance.

Build log says

E _pytest.pathlib.ImportPathMismatchError: ('merge_sort', '/home/travis/build/TheAlgorithms/Python/divide_and_conquer/merge_sort.py', PosixPath('/home/travis/build/TheAlgorithms/Python/sorts/merge_sort.py'))

There are two files named merge_sort.py so Travis is confused. There is this:
https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_sort.py
And there is this:
https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/mergesort.py
You changed the name of the second one, so now they are both named merge_sort.py.
It's possible that whoever wrote these two files named them differently on purpose to avoid this issue. I'm guessing changing the name back to mergesort.py will fix the issue?

@cclauss
Copy link
Member

cclauss commented Sep 19, 2020

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:
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:

"""
if left is None or left is False or left < 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:

@LuisEnMarroquin
Copy link
Author

@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

@cclauss
Copy link
Member

cclauss commented Sep 20, 2020

>>> 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

@LuisEnMarroquin
Copy link
Author

LuisEnMarroquin commented Sep 21, 2020

@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

Please run this, or operator doesn't work the same way inside def

Executed on:

  • Ubuntu Server - Python 3.6.9
  • Windows 10 Pro - Python 3.8.5

@LuisEnMarroquin
Copy link
Author

LuisEnMarroquin commented Sep 21, 2020

After adding your suggested changes I get maximum recursion depth exceeded

image

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()

@stale
Copy link

stale bot commented Oct 21, 2020

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.

@stale stale bot added the stale Used to mark an issue or pull request stale. label Oct 21, 2020
@stale
Copy link

stale bot commented Nov 24, 2020

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!

@stale stale bot closed this Nov 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Used to mark an issue or pull request stale.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants