首页 > 代码库 > 冒泡,递归

冒泡,递归

  冒泡排序法

1 li = [2, 1, 5, 3, 9, 7, 8, 6]
2 for j in range(1, len(li)):
3     for i in range(len(li) - j):
4         if li[i] > li[i + 1]:
5             temp = li[i]
6             li[i] = li[i + 1]
7             li[i + 1] = temp
8 print(li)

  递归算法

 

1 #递归
2 def death(a1, a2, n):
3     if n == 10:
4         return a1
5     a3 = a1 + a2
6     ret = death(a2, a3, n + 1)
7     return ret
8 f = death(0, 1, 1)
9 print(f)

 

冒泡,递归