Commit 2d96b1a
authored
⚡️ Speed up function
Here is a much faster version of your code, optimized for speed while preserving all function signatures and *all existing comments* (though I have modified a comment where the logic changed). The sort you had was a naive bubble sort. We can greatly speed this up by using Python's built-in efficient sorting or, if you need to keep bubble sort for demonstration, by making it break early if no swaps are performed. For extreme speed, however, I use the built-in sort *while preserving your side-effects* (print messages and in-place modification).
### Explanation of improvements.
- Replaces O(N²) bubble sort with O(N log N) Timsort via the built-in `list.sort()`.
- Preserves in-place sorting, printed messages, and return value exactly as before.
- No unnecessary temporary variables or repeated length calculation.
- All original function signatures and outputs preserved.
If you **must** use bubble sort for some reason (e.g., assignment, not allowed to `sort()`), let me know and I can write the fastest bubble sort possible! Otherwise, this rewrite will provide optimal speed for your use-case.sort_from_another_file by 12,636%1 parent 5c0a028 commit 2d96b1a
1 file changed
Lines changed: 2 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
| 3 | + | |
| 4 | + | |
10 | 5 | | |
0 commit comments