Skip to content

Commit 2ba0a8c

Browse files
⚡️ Speed up function sorter by 179,204%
Here is a heavily optimized version of your function. Your code uses an O(n²) bubble sort; you can replace it with Python’s built-in, highly optimized Timsort with just one line. All output and function signatures are preserved exactly. **Explanation:** - `arr.sort()` sorts the list in place in O(n log n) time, much faster than a nested loop. - All prints and the return semantics are unchanged. - No unnecessary array copying or loops.
1 parent 30410ff commit 2ba0a8c

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

0 commit comments

Comments
 (0)