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

Update merge_insertion_sort.py #5833

Merged
merged 4 commits into from
Dec 16, 2021
Merged

Update merge_insertion_sort.py #5833

merged 4 commits into from
Dec 16, 2021

Conversation

yellowsto
Copy link
Contributor

@yellowsto yellowsto commented Nov 17, 2021

Fixes #5774

merge_insertion_sort

Co-Authored-By: AilisOsswald 44617437+AilisOsswald@users.noreply.github.com

Describe your change:

We fixed the Issue of Merge-insertion-Sort not sorting correctly.

We have seen that there is already a PR but we would like to present our fix since it is a little bit different: reidstrieker@2ab90df

We have left all Code as it was before, except line 163
original:
if result[i] == collection[-i]:
corrected:
if result[i] == collection[-1]:

because as the flag name states we want to check if the current item in result is the last item of our input list. (which would be collection[-1] not [-i]

We think that both solutions should work, but the solution with checking (as described above) should perform a tiny bit better.
Normally we do this, since we know everything left to the current value is smaller than the value we will have to sort in:
result = result[: i + 1] + binary_search_insertion(result[i + 1 :], pivot)

If we find the last_odd_item in our result list, we know the same thing like above and we know that there is one additional value, which is already sorted - the last_odd_item. And with that knowlegde we can make the list we have to search through smaller and the list we don't have to look at larger by one each:
result = result[: i + 1 +1] + binary_search_insertion(result[i + 1 :], pivot)

Keeping this flag in the code should make it a bit quicker, since in some cases the list to be searched through is one element smaller.

Our next step would be writing more tests as suggested in this issue #5774. Should this be done in the same issue or in a new one?

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

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

Fixes TheAlgorithms#5774

merge_insertion_sort

Co-Authored-By: AilisOsswald <44617437+AilisOsswald@users.noreply.github.com>
@ghost ghost added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Nov 17, 2021
Copy link
Member

@MaximSmolskiy MaximSmolskiy left a comment

Choose a reason for hiding this comment

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

Unfortunately, #5832 looks more correctly. I tested both solutions on all permutations of range(n) up to n equal to 10. #5832 passes them all. But this doesn't pass. For example, merge_insertion_sort([2, 3, 1, 0]) = [0, 2, 1, 3], but the correct answer is [0, 1, 2, 3]

Fixes TheAlgorithms#5774

merge_insertion_sort

Co-Authored-By: AilisOsswald <44617437+AilisOsswald@users.noreply.github.com>
@yellowsto
Copy link
Contributor Author

Thank you for the hint! We found out why our solution didn't work properly and we have updated it:

Now we check if the length of the input is an odd number, so that the if only triggers if that is the case.

Furthermore we have tested this on all permutations from range(0,10).

Copy link
Member

@MaximSmolskiy MaximSmolskiy left a comment

Choose a reason for hiding this comment

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

Yes, now it passes these tests

Fixes TheAlgorithms#5774

added permutation range from 0 to 4

Co-Authored-By: AilisOsswald <44617437+AilisOsswald@users.noreply.github.com>
@Rohanrbharadwaj
Copy link
Contributor

Looks good, don't mind adding a doctest.testmod() towards the end.

@yellowsto
Copy link
Contributor Author

Could any one have a look please? We would appreciate if we could have a feedback :)

@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Dec 16, 2021
@ghost ghost added the awaiting reviews This PR is ready to be reviewed label Dec 16, 2021
@poyea
Copy link
Member

poyea commented Dec 16, 2021

Let's use all if you don't mind. Otherwise, I'll merge this later. Thank you for your pull request!🤩

@poyea poyea added the awaiting merge This PR is approved and ready to be merged label Dec 16, 2021
@ghost ghost removed awaiting reviews This PR is ready to be reviewed awaiting merge This PR is approved and ready to be merged labels Dec 16, 2021
@poyea poyea added the awaiting merge This PR is approved and ready to be merged label Dec 16, 2021
@poyea poyea merged commit 6680e43 into TheAlgorithms:master Dec 16, 2021
@ghost ghost removed the awaiting merge This PR is approved and ready to be merged label Dec 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Merge insertion sort doesn't work
4 participants