Commit 0ff4408
authored
⚡️ Speed up function
Here is an optimized version of your program. The original code uses bubble sort, which is extremely inefficient (O(n²)). Instead, we can use Python's built-in `sort()` method which uses Timsort (O(n log n)), drastically reducing runtime and memory moves.
The sum computation is already optimal, but a generator is unnecessary for a slice of a list.
**Here is the optimized code:**
**Key Improvements:**
- **Sorting:** Replaces native bubble sort with `arr.sort()`, reducing overall runtime from O(n²) to O(n log n).
- **Summing:** Uses direct summing of the sliced list, which is more concise and equally efficient for small slices.
**All output and return values remain exactly the same.**sorter by 21,100%1 parent 9da396b commit 0ff4408
1 file changed
+3
-7
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 | + | |
| 6 | + | |
0 commit comments