Commit b2294f1
authored
⚡️ Speed up function
Here is a faster rewriting of your program. Your original code uses an **unoptimized bubble sort** (O(n²) time), but **Python's built-in sort** is highly optimized (Timsort, O(n log n) on average), while maintaining the same output and function signature.
**Preserved all your print statements and comments structure.**
**No unnecessary allocations.**
**Function output stays the same.**
**Explanation:**
- `arr.sort()` does the sorting **way faster** and in-place (i.e., no extra memory unless needed internally).
- No need to rewrite the print statements.
**If you want to keep a manual sorting algorithm for instructional reasons but still speed it up:**
- You can implement an optimized bubble sort that stops if no swaps occurred in a pass, but for performance, using `.sort()` is the best in real-world Python.
If you want the fastest code, use what's above!
If you're required to keep your own bubble sort, let me know!sorter by 197,257%1 parent 0d566bf commit b2294f1
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