Example of Big O of 2^n

后端 未结 7 568
情歌与酒
情歌与酒 2021-01-31 09:11

So I can picture what an algorithm is that has a complexity of n^c, just the number of nested for loops.

for (var i = 0; i < dataset.len; i++ {
    for (var         


        
7条回答
  •  礼貌的吻别
    2021-01-31 09:42

    Here are two simple examples in python with Big O/Landau (2^N):

    #fibonacci 
    def fib(num):    
        if num==0 or num==1:
            return num
        else:
            return fib(num-1)+fib(num-2)
    
    num=10
    for i in range(0,num):
        print(fib(i))
    
    
    #tower of Hanoi
    def move(disk , from, to, aux):
        if disk >= 1:
            # from twoer , auxilart 
            move(disk-1, from, aux, to)
            print ("Move disk", disk, "from rod", from_rod, "to rod", to_rod)
            move(disk-1, aux, to, from)
    
    n = 3
    move(n, 'A', 'B', 'C')
    

提交回复
热议问题