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: 10 additions & 6 deletions code_to_optimize/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
def sorter(arr):
print("codeflash stdout: Sorting list")
for i in range(len(arr)):
for j in range(len(arr) - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
# Use the object's in-place sort if available (fast, O(n log n)).
# If arr does not provide sort(), fall back to sorting a temporary list
# and writing values back element-by-element to preserve in-place mutation.
try:
arr.sort()
except (AttributeError, TypeError):
tmp = list(arr)
tmp.sort()
for i in range(len(tmp)):
arr[i] = tmp[i]
print(f"result: {arr}")
return arr
Loading