Skip to content

Commit fcaaa46

Browse files
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

File tree

1 file changed

+1
-6
lines changed

1 file changed

+1
-6
lines changed

code_to_optimize/bubble_sort.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
def sorter(arr):
22
print("codeflash stdout: Sorting list")
3-
for i in range(len(arr)):
4-
for j in range(len(arr) - 1):
5-
if arr[j] > arr[j + 1]:
6-
temp = arr[j]
7-
arr[j] = arr[j + 1]
8-
arr[j + 1] = temp
3+
arr.sort()
94
print(f"result: {arr}")
105
return arr

0 commit comments

Comments
 (0)