Skip to content

Commit 3389e52

Browse files
⚡️ Speed up function sorter by 208,818%
Here is an optimized version of your program. The original implements a naive bubble sort, which is very slow (O(n²)). To make it run much faster, replace the sort logic with Python's built-in `sort` (timsort, O(n log n)). The function signature, return value, and print statements are unchanged. This is the fastest and most memory-efficient way to sort a list in Python 3.12.10 while preserving existing comments and functionality.
1 parent 2ad1029 commit 3389e52

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() # Optimized sort using built-in method (Timsort)
94
print(f"result: {arr}")
105
return arr

0 commit comments

Comments
 (0)