周末的下午,闲来无事,望着窗外簌簌的落叶,想起了杜甫的诗句:无边落木萧萧下。编个冒泡排序程序,来派遣我秋日里这莫名的伤感。
#2021.10.23 HIT ATCI lzh
#python数组,冒泡排序法,将first_record从小到大排序
first_record = [18, 8, 7, 2, 3, 6, 1, 1]
print(first_record)
Len = len(first_record)#列表长度
print('first_record 长度 = ', Len)
target_record = first_record#存储从小到大排序后的值
for i in range(Len-1): #只需要循环Len-1次
for j in range(i+1, Len, 1):
if first_record[i] > first_record[j]:
_ = first_record[j]
first_record[j] = first_record[i]
first_record[i] = _
print("第{0}次排序结果:".format(i+1))
print(first_record)
print("Success!")
print("最终结果:")
print(target_record)
输出结果:
[18, 8, 7, 2, 3, 6, 1, 1]
first_record 长度 = 8
第1次排序结果:
[1, 18, 8, 7, 3, 6, 2, 1]
第2次排序结果:
[1, 1, 18, 8, 7, 6, 3, 2]
第3次排序结果:
[1, 1, 2, 18, 8, 7, 6, 3]
第4次排序结果:
[1, 1, 2, 3, 18, 8, 7, 6]
第5次排序结果:
[1, 1, 2, 3, 6, 18, 8, 7]
第6次排序结果:
[1, 1, 2, 3, 6, 7, 18, 8]
第7次排序结果:
[1, 1, 2, 3, 6, 7, 8, 18]
Success!
最终结果:
[1, 1, 2, 3, 6, 7, 8, 18]
有时候,快乐就是这么简单。