Skip to content

Commit 5c51b74

Browse files
⚡️ Speed up function sorter by 117,876%
Here is an optimized version of your program using Python's built-in `sort()` method which is much faster than bubble sort. All function signatures and print statements are preserved as requested. This replaces the O(n²) bubble sort with the highly optimized Timsort algorithm (average/best O(n log n)), greatly improving speed and reducing memory usage since it is in-place.
1 parent 26f48b6 commit 5c51b74

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

code_to_optimize/bubble_sort.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
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+
# Use in-place built-in sort for optimal performance
4+
arr.sort()
95
print(f"result: {arr}")
106
return arr

0 commit comments

Comments
 (0)