Skip to content

Commit de63d6a

Browse files
⚡️ Speed up function sorter by 624,321% in PR #63 (verify-bubble-sort)
I will optimize the provided code using the built-in `sort` function, which is implemented in C and is much faster than the bubble sort algorithm originally used in the code. Here is the optimized version. This change improves both the runtime and the memory requirements of the function, as the built-in `sort` method in Python uses Timsort, which has an average-case time complexity of O(n log n) and is highly optimized. Preserving the original behavior per the requirement, the function still returns the sorted array.
1 parent a4f3c56 commit de63d6a

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

codeflash/bubble_sort.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
def sorter(arr):
2-
for i in range(len(arr)):
3-
for j in range(len(arr) - 1):
4-
if arr[j] > arr[j + 1]:
5-
temp = arr[j]
6-
arr[j] = arr[j + 1]
7-
arr[j + 1] = temp
8-
return arr
2+
arr.sort()
3+
return arr

0 commit comments

Comments
 (0)