Commit 316627d
authored
⚡️ Speed up function
The optimized code replaces a manual bubble sort implementation (O(n²)) with Python's built-in `arr.sort()` method, which uses Timsort - a highly optimized hybrid sorting algorithm with O(n log n) complexity.
**Key changes:**
- **Eliminated nested loops**: The original bubble sort required ~115M iterations for large inputs, now replaced with a single `arr.sort()` call
- **Algorithm complexity improvement**: From O(n²) bubble sort to O(n log n) Timsort
- **Leveraged native implementation**: Python's sort is implemented in C and heavily optimized
**Why this is faster:**
- **Algorithmic advantage**: For 1000 elements, bubble sort needs ~1M comparisons vs ~10K for Timsort
- **Native C implementation**: Built-in sort runs at C speed rather than interpreted Python
- **Adaptive optimizations**: Timsort performs even better on partially sorted data
**Performance characteristics from tests:**
- **Small arrays (≤10 elements)**: 37-69% faster due to reduced overhead
- **Large arrays (1000 elements)**: 9,000x-106,000x faster due to algorithmic superiority
- **Best case scenarios**: Already sorted lists see 62,000x+ speedup
- **Worst case scenarios**: Reverse-sorted lists see 106,000x+ speedup
The optimization maintains identical behavior including in-place sorting, error handling for incomparable types, and all output formatting.sorter by 177,794%1 parent 0d93128 commit 316627d
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