Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions code_to_optimize/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
def sorter(arr):
print("codeflash stdout: Sorting list")
for i in range(len(arr)):
for j in range(len(arr) - 1):
n = len(arr)
for i in range(n):
swapped = False
# After each i, the last i elements are in place
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
# Swap elements directly, no temp necessary
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
# If no two elements were swapped by inner loop, then array is sorted
if not swapped:
break
print(f"result: {arr}")
return arr
Loading