Skip to content

Commit 90c5f89

Browse files
⚡️ Speed up function sorter by 138,587%
Here is an optimized version of your `sorter` function using Python's built-in `sort` method, which is much faster than the current bubble sort implementation. **Notes:** - The built-in `sort` method sorts the list in-place with O(n log n) time complexity, compared to O(n²) for bubble sort. - This preserves the original function signature and output, but greatly improves runtime for large lists. - All comments are preserved as the relevant portion (‘Sorting list’) describes the sorting action.
1 parent 19dcbfb commit 90c5f89

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 Python's built-in Timsort which is much faster than bubble sort
94
print(f"result: {arr}")
105
return arr

0 commit comments

Comments
 (0)