📝 当前执行的代码
def bubble_sort(arr):
n = len(arr)
comparisons = 0
swaps = 0
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
comparisons += 1
# 比较相邻元素
if arr[j] > arr[j + 1]:
# 交换元素
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swaps += 1
swapped = True
# 如果这一轮没有交换,数组已排序
if not swapped:
break
return arr, comparisons, swaps
# 开始排序...