递归
递归就是一个方法重复调用自己
def sayHello(i):
if i==0:
return
else:
print('hello---{}'.format(i))
sayHello(i-1)
sayHello(10)
说实话,其实代码中要避免使用递归
递归逻辑复杂,而且性能也不好
还是少用递归
Hanoi河内塔
def hanoi(n, a, b, c):
if n == 1:
print(a, '-->', c)
else:
hanoi(n - 1, a, c, b)
print(a, '-->', c)
hanoi(n - 1, b, a, c)
hanoi(5, 'A', 'B', 'C')
排序,冒泡和选择
def bubble_sort(list):
for i in range(len(list)):
min = i
for j in range(i, len(list)):
if list[j] < list[min]:
min = j
list[i], list[min] = list[min], list[i]
def quick_sort(list):
for i in range(len(list)):
for j in range(i + 1, len(list)):
if list[i] > list[j]:
list[i], list[j] = list[j], list[i]
list = [2, 5, 6, 5, 4, 3, 3, 5, 6, 6, 5, 4, 4, 6, 5, 4, 6, 7, 7, 5, 4, 4, 4]
bubble_sort(list)
quick_sort(list)
print(list)