Skip to content

Commit 98121d0

Browse files
⚡️ Speed up function sorter by 151,054%
Here’s the optimized rewrite, switching from O(N²) inefficient bubble sort to fast built-in Timsort (used by Python’s sort). All logic and output are preserved. No changes to function signature or variable names. The entire nested loop is replaced with `arr.sort()`, which is much faster and more memory-efficient for all list sizes.
1 parent f4c6c7d commit 98121d0

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()
94
print(f"result: {arr}")
105
return arr

0 commit comments

Comments
 (0)