Commit 29bf14d
authored
⚡️ Speed up method
The optimization replaces a manual bubble sort implementation with Python's built-in `arr.sort()` method, delivering a massive **107,319% speedup**.
**Key Changes:**
- **Algorithm replacement**: Swapped O(n²) bubble sort with Python's built-in Timsort (O(n log n) average case)
- **Implementation efficiency**: Python's `.sort()` is implemented in C and highly optimized for real-world data patterns
- **Preserved behavior**: Maintains in-place sorting and returns the same list object, preserving the original contract
**Why This Works:**
The original bubble sort performs nested loops with explicit element comparisons and swaps in Python bytecode. For a 1000-element list, this requires ~500,000 comparisons in the worst case. Python's Timsort leverages:
- Native C implementation (orders of magnitude faster than Python loops)
- Adaptive algorithms that perform better on partially sorted data
- Optimized memory access patterns
**Performance by Test Case:**
- **Small lists (2-10 elements)**: 60-140% faster due to reduced overhead
- **Large sorted lists**: 696,790% faster - Timsort detects already-sorted sequences
- **Large reverse-sorted lists**: 1,009,760% faster - Timsort handles this pattern efficiently
- **Large random lists**: 70,000%+ faster - O(n log n) vs O(n²) complexity advantage
The optimization is particularly effective for larger datasets where the algorithmic complexity difference dominates, while still providing consistent improvements across all test cases.Sorter.sort_static by 107,319%1 parent 8753e54 commit 29bf14d
1 file changed
+6
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
| |||
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| 14 | + | |
12 | 15 | | |
13 | 16 | | |
14 | 17 | | |
| |||
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
| 33 | + | |
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
33 | 37 | | |
| 38 | + | |
34 | 39 | | |
35 | 40 | | |
36 | 41 | | |
| |||
44 | 49 | | |
45 | 50 | | |
46 | 51 | | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
| 52 | + | |
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| |||
0 commit comments