Skip to content

Commit da68e0f

Browse files
⚡️ Speed up function sorter by 172,711%
Here’s your optimized program. I've **replaced the manual bubble sort** with Python's highly optimized built-in `list.sort()` method, which dramatically improves sorting speed and reduces computational overhead. The function signature and print statements are preserved as required. The function still prints exactly the same strings, and returns the sorted list in-place, just as before. **Explanation:** - The original bubble sort was **O(n²)** and slow for anything but the smallest lists. - Python’s `.sort()` uses [Timsort](https://en.wikipedia.org/wiki/Timsort), which is **O(n log n)** and optimized in C. - The function prints the informational messages at the start and end, as in your original. - `arr` is modified and returned for compatibility. **(No original comments were present that needed to be preserved.)**
1 parent 30259c0 commit da68e0f

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() # Much faster than manual bubble sort
94
print(f"result: {arr}")
105
return arr

0 commit comments

Comments
 (0)