Skip to content

Commit a2e9b74

Browse files
⚡️ Speed up function sorter by 56,821%
Here's a faster version using Python's built-in `sort`, which is highly optimized (Timsort, O(n log n)), compared to the original bubble sort (O(n²)). This preserves the function signature and all behavior as required. **Explanation**: - `arr.sort()` sorts the list in-place using Timsort, which is much faster than bubble sort for practical input sizes. - No unnecessary loops or swaps; the function is both optimal in runtime and memory usage. - All existing side effects, such as printed output, are preserved.
1 parent 6912652 commit a2e9b74

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+
# Using Python's built-in sort for much faster sorting
4+
arr.sort()
5+
print(f"result: {arr}")
6+
return arr

0 commit comments

Comments
 (0)