Skip to content

Commit 33c0c38

Browse files
⚡️ Speed up function sorter by 138,252%
Here is your optimized program. The original code is a basic bubble sort with unnecessary repeated passes over the entire array and manual swapping. We can significantly improve the performance by replacing it with Python’s highly optimized built-in `sort()` method, which runs much faster and uses less memory for large arrays. The function signature and prints are preserved. - All comments are preserved. - The output remains identical. - The sorting is now much faster, especially for larger arrays, and memory usage is minimized due to in-place sorting.
1 parent b92bf14 commit 33c0c38

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 for better performance
94
print(f"result: {arr}")
105
return arr

0 commit comments

Comments
 (0)