Commit fcaaa46
authored
Optimize sorter
What changed:
- Replaced the Python-level bubble sort (two nested loops with element-by-element swaps) with the built-in list.sort().
Why it’s faster:
- Algorithmic complexity: Bubble sort is O(n^2) regardless of input; list.sort() (Timsort) is O(n log n) on average/worst and O(n) on partially/fully sorted data. This eliminates the quadratic work visible in the profiler (tens of billions of loop iterations and ~31M swaps).
- C implementation: list.sort() runs in optimized C, avoiding Python interpreter overhead from tight loops, repeated indexing, comparisons, and assignments. The original spends the majority of time in Python bytecode; the optimized version moves that work into C. The profiler shows the remaining time dominated by printing, not sorting.
- Fewer operations: No repeated range/len evaluations or per-iteration bounds checks at Python level; comparisons and moves are batched internally by Timsort.
Behavioral considerations:
- In-place semantics preserved: arr.sort() sorts in place; function still returns arr, matching the original’s side effects and return.
- Stability preserved: Python’s sort is stable, equivalent to the original adjacent-swap behavior for equal keys.
- Type behavior: Mixed int/float works; int/str raises TypeError in both versions (raised within sort), matching tests.
Evidence from tests and profiles:
- Line profiler: Original spends ~50s, dominated by nested loops and swaps; optimized total ~3.6ms with sorting itself taking microseconds; printing is now the majority of time.
- Large inputs: Massive gains on already sorted, reverse, random, and duplicates (20k%–97k% faster). Timsort’s O(n) on runs explains the exceptional speedups on sorted/partially-sorted and duplicate-heavy cases. Even worst-case random inputs drop from tens of ms to ~100–200µs.
Best-suited cases:
- Large lists, especially already sorted, nearly sorted, or with long runs/duplicates.
- All general numeric lists (ints/floats) as in tests.
Net: The speedup comes from replacing an O(n^2) Python-loop algorithm with an O(n log n)/O(n) C-optimized sort, slashing interpreter overhead and operation count.1 parent 04ab4af commit fcaaa46
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