Commit b389fa3
authored
⚡️ Speed up method
The optimization replaces a manual bubble sort implementation with Python's built-in `sort()` method, delivering massive performance gains.
**What was optimized:**
- Replaced the nested O(n²) bubble sort loops with `self.arr.sort()`, which uses Timsort (O(n log n) average case)
- Eliminated manual element swapping and redundant array accesses
- Preserved exact behavior: sorts `self.arr` in-place and returns `self.arr * multiplier`
**Why this is dramatically faster:**
1. **Algorithmic complexity**: Timsort is O(n log n) vs bubble sort's O(n²), making it exponentially faster for larger inputs
2. **Native implementation**: Python's `sort()` is implemented in highly optimized C code
3. **Reduced overhead**: Eliminates Python loop overhead and multiple array index operations per comparison
**Test case performance patterns:**
- **Small arrays (2-4 elements)**: 70-160% speedup due to reduced Python overhead
- **Large arrays (100-1000 elements)**: 15,000x to 1,000,000x+ speedup due to algorithmic efficiency
- **Already sorted data**: Massive gains since Timsort has O(n) best-case performance vs bubble sort's O(n²) regardless of input order
- **Reverse sorted data**: Most dramatic improvements (1,000,000x+) as this is bubble sort's worst case
The optimization maintains identical sorting behavior and return value semantics while delivering the most significant performance improvement for larger datasets where the O(n²) vs O(n log n) complexity difference dominates.Sorter.sorter by 205,046%1 parent 8753e54 commit b389fa3
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 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| |||
0 commit comments