Skip to content

Commit df66b7d

Browse files
⚡️ Speed up function sorter by 44,398%
The current implementation of the `sorter` function uses Bubble Sort, which has a worst-case time complexity of O(n^2). This can be improved to O(n log n) by using the built-in sorting function in Python, which is highly optimized. Here's the updated code. Python's built-in `sort` function uses Timsort, which has a time complexity of O(n log n). This significantly improves the performance over the original Bubble Sort implementation. The function signatures and print statements are preserved as requested.
1 parent 77f43a5 commit df66b7d

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() # Utilizing Python's built-in Timsort algorithm
94
print(f"result: {arr}")
105
return arr

0 commit comments

Comments
 (0)