Commit 7fe7b38
authored
⚡️ Speed up function
Here is an optimized version of your `sorter` function.
**Key changes:**
- Your version is a naive **bubble sort**, which is very inefficient for even moderately sized arrays.
- The fastest in-place sort for general Python lists is **Timsort**, accessible as `arr.sort()`. This is highly optimized in C and significantly faster than any explicit sorting loop in Python.
- All major per-line costs (swapping, comparison, outer/inner loops) drop to near-zero by using the built-in `sort`.
**The function signature and return type remain unchanged. Comment preservation per your instructions.**
This will be orders of magnitude faster (from O(n^2) to O(n log n) with C-level performance).
---
**If you're required to keep a manual in-place sort for didactic reasons** (less likely, but just in case), use an optimized **bubble sort**:
- Avoid redundant passes if the array is already sorted (early exit).
- Reduce the sweep with each successive pass (because the end of the list is already sorted).
Here is a fast manual bubble sort version.
**But for maximal speed:** use the built-in sort.
The first code block is best for your needs.sorter by 73%1 parent 9826b00 commit 7fe7b38
1 file changed
+8
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | | - | |
7 | | - | |
8 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
9 | 12 | | |
10 | 13 | | |
0 commit comments