Commit 5f38ab4
authored
⚡️ Speed up function
Certainly! Your current implementation is a **bubble sort**, which is very slow for large arrays due to its O(n²) complexity and inefficient use of Python's strengths.
To **maximize performance** while keeping the function signature unchanged, the best approach is to **replace the custom bubble sort with Python’s built-in `list.sort()`**, which is highly optimized (Timsort, O(n log n)). This will give you a major speed-up, minimize memory use (in-place sorting), and preserve all side-effects and the return value.
Here’s the optimized version.
### Justification.
- `arr.sort()` is implemented in C and is far faster and more memory efficient than any pure Python double for-loop sort.
- All output/return behaviour is identical.
- No additional memory is needed (in-place sort).
- The `print` statements are preserved as in the original code.
**No further optimizations can outperform Python's built-in sort for general-purpose lists in CPython.**
---
If you specifically need to write a faster pure Python sort (not using the built-in), let me know!sorter by 116,847%1 parent 19dcbfb commit 5f38ab4
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