Skip to content
MisakaTang's Blog
Go back

MIT-CS6.00笔记Lec7-9

Edit page

Lec7

Python数组:

解释: L2 = L1 ==> 创建了一个L2和L1进行绑定,事实是L1和L2绑定了同一个对象,所以在通过L1改变了该对象的时候,L2也会发生变化,其实不是L1,L2的变化而是他们共同绑定的对象的变化.
Dictionaries(字典):

Pseudo code(伪代码):利用伪代码来控制程序流,拆分模块以及搞清对数据的操作

模块性和封装:不需要关心内部的具体实现,只需要调用就可以

efficiency-orders of growth(算法复杂度,运行效率):

Space & Time:

Random access model:假定我们在读取任意一块内存区域的时候花费的时间是恒定的(理想状态)

Lec8

例子 整数a的b次幂的计算:

Iterative exponent:

def exp1(a,b):    
    ans = 1    
    while (b>0):        
        ans += a        
        b -= 1    
    return ans

总共进行了2+3b步
O(b) -> liner

复杂度增长:

Recursive exponent:

def exp2(a,b):    
    if b == 1:        
        return a    
    else:        
        return a*exp2(a,b-1)

分析递归式程序的复杂度:

t(b) = 3 + t(b-1)     
     = 3 + 3 + t(b-2)     
     = 3*k + t(b-k)     
     Done b-k = 1

==> O(b)

二分:

a ** b = (a*a) ** (b/2) -> b even       
       = a*(a**(b-1)) -> b odd
def exp3(a,b):    
    if b == 1:        
        return a    
    if (b%2)*2 == b:        
        return exp3(a*a, b/2)    
    else:        
        return a*exp3(a,b-1)

==> O(log b)

b even t(b) = 6+t(b/2)
b odd  t(b) = 6+t(b-1)    
     ==>t(b) = 12 + t(b/2)            
             = 12k + t(b/2**k)        
    k = log2(b)

汉诺塔:

Lec9

二分查找算法:
思想:

缺点: 列表(list)不能用二分查找(效率低,复杂度变成线性复杂度)

Generalize:

  1. pick the mind point
  2. check to see if this is answer
  3. if not, reduce to smaller problem.Repeat

Question:

Amortize the cost:(平摊花费),如果只搜索一次的话,liner search效率更高,如果搜索多次的话,那么先排序就会体现优势在平摊了排序的花费,而查找的效率是logn.

排序算法


Edit page
Share this post on:

Previous Post
MIT-CS6.00笔记Lec10-12
Next Post
由一篇博客的讨论所联想的..