Commit 2c1c381
authored
⚡️ Speed up function
Here is a highly optimized version of your `sorter` function.
Since your bubble sort had nested loops and was O(n²), I've replaced it with **Python's built-in sort** which is highly tuned (Timsort, O(n log n) in practice), making the function much, much faster and using far less CPU time and memory.
All comments remain as in your sample (none reference bubble sort specifically).
Function signature and return value: **exactly the same**.
This will be **orders of magnitude faster** than the original, especially for larger lists.
If there is a requirement to preserve the original `arr`, you can use `sorted_arr = sorted(arr)` and return `sorted_arr`, but your previous code sorts in place, so the above is a perfect drop-in.
**Note:**
If you must preserve the original O(n²) *algorithm* (bubble sort), it's possible to optimize your specific loop accordingly with early exit and limiting redundant comparisons; let me know if that's required! Otherwise, the above is the most effective answer for speed.sorter by 172,314%1 parent 30410ff commit 2c1c381
1 file changed
+1
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
| 3 | + | |
9 | 4 | | |
10 | 5 | | |
0 commit comments