Performance: replace custom sort by native sort#605
Merged
niknetniko merged 2 commits intomasterfrom Jan 24, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request refactors the sorted_no_duplicates function in utils.py to use Python's native sorted() function with cmp_to_key instead of a custom TimSort implementation. The change delivers ~30-50% performance improvement while simplifying the code significantly. The PR also fixes a bug in the TypeError fallback path where a boolean was incorrectly returned instead of an integer comparison result.
Changes:
- Replaced custom TimSort/insertion sort implementation with Python's built-in
sorted()usingcmp_to_key - Fixed TypeError fallback in the
order()function to return proper comparison integers instead of boolean - Simplified duplicate filtering logic to be more straightforward
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
niknetniko
approved these changes
Jan 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request replaces the custom sort in utils.py by the built-in sort with
cmp_to_key. There's a slight difference in output: the new version is stable whereas the original was not. All tests still pass.In addition, I fixed a small bug in order fallback.
Performance of the function itself is ~50% better. When profiling the function in an actual exercise, the improvement was closer to 30%. Probably because it is then often called on (very) small lists. For exercises using sets, the impact is more noticable.
Next to the performance benefic, this simplifies the code.