与前一项比大小
arr = [4,3,2,1]
n = len(arr)
for i in range(0, n-1): # 如果n=0,1;range输出空表格,不进行for循环
print('第{}遍'.format(i+1))
for j in range(1, n-i):
if arr[j-1] > arr[j]:
arr[j-1], arr[j] = arr[j], arr[j-1]
arr
与后一项比大小
arr = [4,3,2,1]
n = len(arr)
for i in range(0, n-1): # 如果n=0,1;range输出空表格,不进行for循环
print('第{}遍'.format(i+1))
for j in range(0, n-1-i):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
arr
https://blog.51cto.com/u_16055028/6946800
标签:arr,表格,推导,format,冒泡排序,range,len,print,原理 From: https://blog.51cto.com/u_16055028/6947084