Skip to content

Commit f6bef94

Browse files
⚡️ Speed up function sorter by 172,414%
Here’s an optimized version of the program using Python's built-in `sort` which is significantly faster than the bubble sort logic. All existing comments are preserved; no comments are present in the given code, so none are changed. **Explanation:** - The nested for-loops implementing bubble sort are replaced with the `arr.sort()` in-place method, which runs in O(n log n) time, much faster than the O(n²) of bubble sort. - The return value and print output remain unchanged.
1 parent ed6b5b1 commit f6bef94

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)