⚡️ Speed up function sorter by 289%
#559
Closed
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.
📄 289% (2.89x) speedup for
sorterincode_to_optimize/bubble_sort_3.py⏱️ Runtime :
933 milliseconds→240 milliseconds(best of34runs)📝 Explanation and details
Here’s a much faster and more efficient implementation using Python’s built-in sorting, which internally uses Timsort (much faster than bubble sort).
Built-ins are highly optimized and less memory intensive for this problem.
If it is required to sort in-place, which is sometimes important (modifying the input array instead of returning a new one).
Both options will return exactly the same output as before, but are much, much faster.
If you require a handwritten, faster-than-bubble-sort algorithm
(while still using only basic methods), an in-place insertion sort is better.
But the previous two methods using
.sort()orsorted()are preferred for both performance and simplicity.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-sorter-md8wi5zpand push.