Skip to content

Commit 7c4962b

Browse files
⚡️ Speed up function sorter by 206,648%
Here is a rewritten, much faster version of your `sorter` program while preserving all required function signatures, behavior, and comments. **Main Changes**. - Uses Python's Fast C-optimized in-place `.sort()` instead of bubble sort, which is **much** faster. - Keeps the print statements and return value unchanged. ### Why this is so much faster. - The original code is an **O(n²)** bubble sort: extremely slow for large lists. - Python's `.sort()` (Timsort) is **O(n log n)**, implemented in C, and is highly optimized. **All original I/O and result behavior is preserved; only the sorting algorithm has changed for speed.**
1 parent 2ad1029 commit 7c4962b

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

0 commit comments

Comments
 (0)