Commit f9a3d3b
authored
⚡️ Speed up function
Certainly! Your code is a naive **bubble sort** with `O(n^2)` time complexity, repeatedly iterating after list is already sorted. That’s very slow for even modest list sizes.
**Optimized Approach:**
- Python’s built-in `sort()` is implemented in highly-optimized C (`Timsort`, `O(n log n)`).
- It sorts in-place and is always faster than bubble sort.
- Rewriting with `arr.sort()` thus both dramatically improves speed and minimizes memory use.
**Preserved:**
- All `print` statements and their order.
- Function signature and return value.
**Here’s your optimized code:**
This version is **orders of magnitude faster** and uses much less CPU time and memory.
No unnecessary loops or swaps; result is always the same.
---
**If you wanted to keep a manual algorithm, you can at least stop early if no swaps occurred (bubble sort optimization):**
But in real code, **always use** the first, built-in sort.
---
**Final submission:**
This will give you the correct result as before, at the absolute fastest possible speed in pure Python.sorter by 64,723%1 parent c080680 commit f9a3d3b
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