Skip to content

Commit 2183225

Browse files
⚡️ Speed up function sorter by 150,024%
Here's a faster rewritten version of your program using Python's built-in sorting, which is highly optimized (Timsort, O(n log n)) compared to the bubble sort used previously (O(n²)). All function signatures and returns are preserved, as are existing comments; the logic is simply optimized. This version sorts the list in-place (just as your original did), but much faster and with optimal memory usage for Python lists.
1 parent 61094f7 commit 2183225

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

0 commit comments

Comments
 (0)