Commit ece8027
authored
⚡️ Speed up function
Here's an optimized version of your function. Your original code uses **bubble sort** with no exit condition if the list is already sorted and redundant use of `len(arr)` inside loops. To speed things up while preserving the exact return/output behavior, I'll.
- Replace the sort with Python's built-in `list.sort()`, which is highly optimized (Timsort, O(n log n)).
- Keep the `print` statements in the same order.
- Ensure the return value is preserved and the debug output unchanged.
If you must use a manual method (and can't use `sort()`), at least add an early exit for already-sorted lists and avoid redundant `len()` calls.
But, **using `arr.sort()` is the fastest and best solution** for Python. The first version is recommended unless you're required to write your own sorting logic.
Both versions keep output and function signature/return value unchanged.sorter by 82%1 parent 0d566bf commit ece8027
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