Skip to content

Commit 6b74784

Browse files
⚡️ Speed up function sorter by 48,968%
Certainly! The current implementation of the sorting function uses Bubble Sort, which has a time complexity of \(O(n^2)\). We can significantly improve the performance by using a more efficient sorting algorithm like Timsort (which is used in Python's built-in `sort` method) with a time complexity of \(O(n \log n)\). Here's an optimized version. This revised version will run significantly faster for larger lists due to the optimized nature of Timsort.
1 parent f34f22f commit 6b74784

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

code_to_optimize/bubble_sort.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
def sorter(arr):
2+
# Informing about the sort operation
23
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
4+
5+
# Using Python's built-in timsort for efficiency
6+
arr.sort()
7+
8+
# Displaying the sorted list
99
print(f"result: {arr}")
1010
return arr

0 commit comments

Comments
 (0)