Example of Big O of 2^n

后端 未结 7 716
情歌与酒
情歌与酒 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:56

    Here is a code clip that computes value sum of every combination of values in a goods array(and value is a global array variable):

    fun boom(idx: Int, pre: Int, include: Boolean) {
        if (idx < 0) return
        boom(idx - 1, pre + if (include) values[idx] else 0, true)
        boom(idx - 1, pre + if (include) values[idx] else 0, false)
        println(pre + if (include) values[idx] else 0)
    }
    

    As you can see, it's recursive. We can inset loops to get Polynomial complexity, and using recursive to get Exponential complexity.

提交回复
热议问题