Commit 898f388
authored
⚡️ Speed up function
Here’s an optimized version of your program, rewritten for speed. The original used an unoptimized **bubble sort** (O(n²)), and iterated needlessly over the full array even when already sorted portions could be skipped.
Replacing it with Python's built-in `list.sort()` (which is implemented in C and uses [Timsort](https://en.wikipedia.org/wiki/Timsort), O(n log n)), greatly speeds up the code and reduces memory movements.
This change preserves all output and the function signature.
#### Notes.
- `arr.sort()` sorts the input list **in-place** and is equivalent (in result) to your previous logic.
- All print statements and returned value are untouched except for the replaced sorting part.
- No extra memory usage; even less, since no needless swap variables are created.
If you still want to use a bubble sort, it can be further optimized by stopping the sort early if no swaps occur in a pass. If you want to see that code as well, just ask! But for best speed, use `arr.sort()`.sorter by 180,478%1 parent 42f0ada commit 898f388
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