Commit 88286ed
authored
⚡️ Speed up function
Here's a highly optimized rewrite of your sorting program, preserving function signature and output format. Your code implements a classic bubble sort (O(n²)). The most optimized, readily available algorithm in Python is Timsort (the default for `list.sort()`), which is much faster for all realistic data sizes.
**I will:**
- Replace the manual O(n²) bubble sort with `arr.sort()` (in-place Timsort, O(n log n)).
- Avoid unnecessary loops/swaps and temporary variables.
- Keep the print statements in exactly the same positions as original.
**Here’s the optimized code:**
**Why this is dramatically faster:**
- `arr.sort()` is written in C, avoids Python-level loops and does not double-scan the list.
- No memory increase: still in-place.
- All prints, input, and output behavior preserved.
**If you must keep bubble sort, an optimized bubble sort could early-exit if no swaps occur (not as fast as Timsort, but faster than naive bubble sort):**
**But for speed, always prefer the first Timsort-based version.**sorter by 82%1 parent ef010e5 commit 88286ed
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