Skip to content

Commit 99d80f8

Browse files
⚡️ Speed up function sorter by 32,451%
Here’s the most efficient rewrite of your program: - **Replace** the manual insertion sort (O(n²)) with Python’s built-in `.sort()` (Timsort, O(n log n) worst, very fast in practice—*and* stable/in-place!). - **Preserve all outputs and the function signature.** - The internal sort is now optimal for all reasonable inputs, no unnecessary copies or allocations. **No other changes are necessary:** - `.sort()` modifies the list in place and does so optimally. - All function prints and return values are preserved. This is the fastest way to sort a list in Python given your requirements.
1 parent 2185af9 commit 99d80f8

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

codeflash/bubble_sort.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def sorter(arr):
2+
print("codeflash stdout: Sorting list")
3+
# Uses Python's built-in Timsort algorithm, much faster than insertion sort or bubble sort
4+
arr.sort()
5+
print(f"result: {arr}")
6+
return arr

0 commit comments

Comments
 (0)