1.a = a+b
b = a-b
a = a-b
2.a = a^b
b = a^b
a = a^b
3.a ,b = b ,a (Python独有)
4.c =a
a = b
b = c
1.
list = [9, 8, 7, 6, 5, 4, 3, 2, 1,10]
count = len(list)
while count != 1:
i = 0
flag = True #在每一趟都立了一个flag,假设每一趟都没有换行
# for num1 in list:
# if i == count - 1:
# break
while i != count - 1:
if list[i] < list[i + 1]:
flag = False # flag倒了,只要交换了,假设就不成立
list[i], list[i + 1] = list[i + 1], list[i]
i += 1
count -= 1
if flag: #这一趟走完以后,flag依然是True,说明这一趟没有进行过数据交换
break
print(list)
2.
def bubble_sort(nums):
for i in range(len(nums) - 1):
ex_flag = False # 改进后的冒泡,设置一个交换标志位
for j in range(len(nums) - i - 1):
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
ex_flag = True
if not ex_flag:
return nums # 这里代表计算机偷懒成功 (〃‘▽‘〃)
return nums # 这里代表计算机没有偷懒成功 o(╥﹏╥)o
原文:https://www.cnblogs.com/LearningRoad/p/14531886.html