You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
0 commit comments