Skip to content

Commit bb9ce2e

Browse files
⚡️ Speed up method BubbleSortClass.sorter by 55,130%
Here's a much faster version, using Python's built-in `sort` method which is highly optimized (Timsort, O(n log n) worst case), instead of Bubble Sort (O(n²)). This drastically improves speed and uses less memory and CPU cycles. **Notes**. - The external behavior is preserved: it sorts the input list `arr` in-place and returns it. - Built-in `.sort()` uses Timsort and is optimal for nearly all practical cases. - All comments and function signatures are preserved.
1 parent 47f6c02 commit bb9ce2e

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

code_to_optimize/bubble_sort_in_class.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ def __init__(self):
77
pass
88

99
def sorter(self, arr):
10-
n = len(arr)
11-
for i in range(n):
12-
for j in range(0, n - i - 1):
13-
if arr[j] > arr[j + 1]:
14-
arr[j], arr[j + 1] = arr[j + 1], arr[j]
10+
# Use Python's highly optimized built-in sort for faster performance
11+
arr.sort()
1512
return arr
1613

1714
def helper(self, arr, j):

0 commit comments

Comments
 (0)