# 一 、冒泡排序:

n个数从左至右编号从0到n-1,索引0和1比较,如果索引0大,则交换两者位置;

1、冒泡简单实现

``````num_list = [
[1,3,2,6,5,9,8,4,7],
[1,2,3,4,5,6,7,8,9],
[1,2,3,4,9,6,7,8,5],
[2,4,5,1,7,9,6,3,8]
]

nums = num_list[0]                   # [1,3,2,6,5,9,8,4,7]
length = len(nums)                    #获取排序的长度
for i in range(length):
for j in range(length -i -1):       #每比对一遍 找到最大的在最右边
if nums[j] > nums[j+1]:         #如果左边的比右边的大
tmp = nums[j]                   #暂存这个大的数
nums[j] = nums[j+1]        #这个大数的位置换成 右边的较小的数
nums[j+1] = tmp              #把大的数放到右边
#nums[j],nums[j+1] = nums[j+1],nums[j]     #以上三行可写成一行
print(nums)``````

``````
第1次 --> [1,9,8,5,6,7,4,3,2] [1,8,9,5,6,7,4,3,2] [1,8,5,9,6,7,4,3,2] [1,8,5,6,9,7,4,3,2] [1,8,5,6,7,9,4,3,2]
[1,8,5,6,7,4,9,3,2] [1,8,5,6,7,4,3,9,2] [1,8,5,6,7,4,3,2,9]   #第一遍时找出最大的数9 放到N-1位即最后一位
第2次--> [1,5,8,6,7,4,3,2,9] [1,5,6,8,7,4,3,2,9] [1,5,6,7,8,4,3,2,9] [1,5,6,7,4,8,3,2,9] [1,5,6,7,4,3,8,2,9]
[1,5,6,7,4,3,2,8,9]                                           #第二遍时找出最大的8 n-2位 以次类推

第3次 --> [1,5,6,7,4,3,2,8,9] [1,5,6,4,7,3,2,8,9] [1,5,6,4,3,7,2,8,9] [1,5,6,4,3,2,7,8,9]
第4次 --> [1,5,6,4,3,2,7,8,9] [1,5,4,6,3,2,7,8,9] [1,5,4,3,6,2,7,8,9] [1,5,4,3,2,6,7,8,9]
第5次 --> [1,5,4,3,2,6,7,8,9] [1,4,5,3,2,6,7,8,9] [1,4,3,5,2,6,7,8,9] [1,4,3,2,5,6,7,8,9]
第6次 --> [1,4,3,2,5,6,7,8,9] [1,3,4,2,5,6,7,8,9] [1,3,2,4,5,6,7,8,9]
第7次 --> [1,3,2,4,5,6,7,8,9] [1,2,3,4,5,6,7,8,9]
第8次 --> [1,2,3,4,5,6,7,8,9]
第9次 --> [1,2,3,4,5,6,7,8,9] ``````

2、加入统计

``````num_list = [
[1,3,2,6,5,9,8,4,7],
[1,2,3,4,5,6,7,8,9],
[1,2,3,4,9,6,7,8,5],
[2,4,5,1,7,9,6,3,8]
]
nums = num_list[0]
count = 0                  #定义比对次数
count_swap = 0       #定义交换次数
length = len(nums)

for i in range(length):
for j in range(length - i - i):
count += 1
if nums[j] > nums[j + 1]:
tmp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = tmp
#nums[j],nums[j+1] = nums[j+1],nums[j]     #以上三行赞同于这一行
count_swap += 1
Flag = True  # 是否产生交换
if not Flag:  # 在没有 产生交换时退出
break
print(nums, count_swap, count)
``````

3、冒泡排序优化

``````num_list = [
[1,3,2,6,5,9,8,4,7],
[1,2,3,4,5,6,7,8,9],
[1,2,3,4,9,6,7,8,5],
[2,4,5,1,7,9,6,3,8]
]
nums = num_list[0]
count = 0                  #定义比对次数
count_swap = 0       #定义交换次数
length = len(nums)
Flag = False

for i in range(length):
Flag = False
for j in range(length -i -1):
count +=1
if nums[j] >nums[j+1]:
tmp = nums[j]
nums[j] = nums[j+1]
nums[j+1] = tmp
count_swap +=1
Flag = True           #是否产生交换
if not Flag:                 #在没有 产生交换时退出
break
print(nums,count_swap,count)

def bubble_sort(nums):
length = len(nums)
count = 0
swap = 0
for i in range(0, length):
count +=1
for j in range(i + 1, length):
if nums[i] > nums[j]:
nums[i], nums[j] = nums[j], nums[i]
swap +=1
return nums,count,swap
print(bubble_sort(nums))``````

4、冒泡总结:

# 二、鸡兔同笼问题(二元一次方程)

x + y = 10
2x +4y = 32

`````` for x in range(10):         #因为鸡在10以内
for y in range(10):      #兔子也在10以内
a = x + y                  #类似试着尝试
b = 2*x + 4*y
if a == 10 and b == 32:        #即x+y == 10同时 脚数是32时 成立
print("鸡有:%s,兔有:%s" %(x,y))``````

# 三、最大公约数

``````def MaxP(a,b):
aa = a
bb = b
while a != b:
if a >b:
a = a -b
tmp =a
else:
b = b -a
tmp = b

return "%s和%s最大公约数%s" %(aa,bb,tmp)``````

# 四、判断一个数是否为素数

``````def IsS(x):
for i in range(2,x):
if x % i == 0:
return ("%s is not 素数!" %x)
return ("%s is 素数!" %x)
print(isS(99991))``````

# 五、猴子吃桃问题

``````num =1     #第10天还没有吃呢就剩下 1
print("第10天还没有吃呢就剩下 1 个桃子了!")
for day in range(9,0,-1):      #倒推从第九天开始算#
num = (num+1) *2           #现在 剩下的就是前一天加1再乘以2
print("第 %d 天还有 %d 个桃子!" %(day,num))``````

# 留下 一个思考题目:

``````def Ssort(L,a):
L.append(a)
length = len(L)
for i in range(length):
for j in range(i+1,length):
if L[i] > L[j]:
L[i],L[j] = L[j],L[i]
return L``````