Skip to content

Commit bd42727

Browse files
⚡️ Speed up function sorter by 45,504%
You can optimize the sorting part of the code by using a more efficient sorting algorithm such as Timsort, which is the default sorting algorithm in Python's `sorted()` and `sort()` methods. This way, the function will run much faster, especially for larger lists. Here's the rewritten program. The `arr.sort()` method is highly optimized and will generally perform much better than the original bubble sort implementation. The corrected implementation preserves the original structure by keeping the print statements and return value unchanged.
1 parent f4be9be commit bd42727

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

0 commit comments

Comments
 (0)