algorithm:一个计算过程,解决问题的方法
程序设计=数据结构+算法
输入→算法→输出
数据结构就是关系python
用来估计算法运行时间的一个式子,通常来讲时间复杂度高的算法比复杂度低的算法慢算法
print('hello') # O1 无循环 for i in range(n): # O(n) 一层循环 print('hello') for i in range(n): # O(n2) 2层循环 for j in range(n): print('hello') while n > 1: # O(logn) 每次循环减半 print(n) n = n//2
用来评估算法内存占用大小的式子数据结构
递归的两个特色函数
def func1(x): if x > 0: print(x) func1(x - 1) # 3 2 1 def func2(x): if x > 0: func2(x - 1) print(x) # 1 2 3
对上述结果的解释设计
def hanoi(n, a, b, c): ''' 一共n层,盘子从a通过b移动到c ''' if n > 0: hanoi(n - 1, a, c, b) print('moving from %s to %s' % (a, c)) hanoi(n - 1, b, a, c) hanoi(3,'A','B','C') ''' moving from A to C moving from A to B moving from C to B moving from A to C moving from B to A moving from B to C moving from A to C '''