Skip to content

Commit 2f051d1

Browse files
⚡️ Speed up function sorter by 158,006%
Here’s a much faster version of your `sorter` function, using Python’s built‐in `sort` method, which implements Timsort (far more efficient than the original bubble sort loop). **Explanation of optimizations:** - Replaces the O(N²) bubble sort with the highly optimized O(N log N) `list.sort()` built-in. - The function signature and print statements are unchanged per your requirements. - Works *in place* like the original. Return value is identical. If you need to avoid modifying the input list in-place, let me know!
1 parent 9316ee7 commit 2f051d1

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() # Use Timsort for fast in-place sorting
94
print(f"result: {arr}")
105
return arr

0 commit comments

Comments
 (0)