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 an optimized version of your function. Your original code uses **bubble sort** with no exit condition if the list is already sorted and redundant use of `len(arr)` inside loops. To speed things up while preserving the exact return/output behavior, I'll.
- Replace the sort with Python's built-in `list.sort()`, which is highly optimized (Timsort, O(n log n)).
- Keep the `print` statements in the same order.
- Ensure the return value is preserved and the debug output unchanged.
If you must use a manual method (and can't use `sort()`), at least add an early exit for already-sorted lists and avoid redundant `len()` calls.
But, **using `arr.sort()` is the fastest and best solution** for Python. The first version is recommended unless you're required to write your own sorting logic.
Both versions keep output and function signature/return value unchanged.
0 commit comments