-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Conversation
Fixes TheAlgorithms#5774 merge_insertion_sort Co-Authored-By: AilisOsswald <44617437+AilisOsswald@users.noreply.github.com>
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.
Fixes TheAlgorithms#5774 merge_insertion_sort Co-Authored-By: AilisOsswald <44617437+AilisOsswald@users.noreply.github.com>
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). |
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.
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>
Looks good, don't mind adding a |
Could any one have a look please? We would appreciate if we could have a feedback :) |
Let's use |
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?
Checklist:
Fixes: #{$ISSUE_NO}
.