Skip to content
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

Small mistake in sort/merge_sort.py array merge #31

Closed
fooofei opened this issue Apr 26, 2017 · 4 comments
Closed

Small mistake in sort/merge_sort.py array merge #31

fooofei opened this issue Apr 26, 2017 · 4 comments
Labels

Comments

@fooofei
Copy link

fooofei commented Apr 26, 2017

code location: keon/algorithms/blob/master/sort/merge_sort.py

mistake 1

origion code: line - 30

    # Add the left overs if there's any left to the result
    if left:
        arr.extend(left[left_cursor:])
    if right:
        arr.extend(right[right_cursor:])
    return arr

should be ==>

    # Add the left overs if there's any left to the result
    if left_cursor < len(left)::
        arr.extend(left[left_cursor:])
    if right_cursor < len(right)::
        arr.extend(right[right_cursor:])
    return arr

more pythonic efficient way ,no construct sub array ==>

    # Add the left overs if there's any left to the result
    for i in range(left_cursor,len(left)):
        arr.append(left[i])
    for i in range(right_cursor,len(right)):
        arr.append(right[i])
    return arr

mistake 2

origion code : line - 38

print(merge_sort(array, 0, len(array)-1))
print(array)

should be ==>

print(merge_sort(array))
@fooofei fooofei changed the title small mistake in sort/merge_sort.py in array merge small mistake in sort/merge_sort.py array merge Apr 26, 2017
@fooofei fooofei changed the title small mistake in sort/merge_sort.py array merge Small mistake in sort/merge_sort.py array merge Apr 26, 2017
@keon keon added the bug label Apr 27, 2017
@alisha17
Copy link
Contributor

Can I work on this issue?

@keon
Copy link
Owner

keon commented Apr 28, 2017

@alisha17 of course!

@ankit167
Copy link
Collaborator

ankit167 commented Apr 29, 2017

@alisha17 'arr' needs to be returned at the end of the merge() (in the new code)

@fooofei
Copy link
Author

fooofei commented Apr 29, 2017

@ankit167 thanks for point that return arr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants