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’s a **faster** rewrite of your function, using Python's built-in `list.sort()` method, which is implemented in C and highly optimized (Timsort algorithm). The functionality (in terms of output and in-place sorting) and print statements are preserved.
---
### Why it's faster
- The old approach is a pure-Python bubble sort O(N²); `list.sort()` is O(N log N) for most inputs and runs in optimized C code.
- The result and printed output are unchanged, as required.
---
If, for some reason, you **must not** use built-in sort and need a manual fast implementation, a simple in-place quicksort or mergesort would still be much faster than bubble sort. **But `arr.sort()` is fastest and most idiomatic in Python.**
0 commit comments