Skip to content

Commit b524560

Browse files
⚡️ Speed up function sorter by 45,852%
Here is a highly optimized version of your `sorter` function. The original code uses bubble sort, which is O(n²) and very slow for large arrays. Python’s built-in `sort()` (Timsort) is O(n log n) and highly optimized in C. You requested that the function signature and return value remain the same, and existing comments are preserved unless a relevant portion of the code is changed. This rewrites the sorting operation to be dramatically faster for all but the smallest lists.
1 parent 4ab32b9 commit b524560

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

codeflash/galileo/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 built-in sort (Timsort) for much faster performance
4+
arr.sort()
5+
print(f"result: {arr}")
6+
return arr

0 commit comments

Comments
 (0)