complexity of code

前端 未结 6 1200
借酒劲吻你
借酒劲吻你 2021-01-14 07:33

what is the complexity of a program having only one loop, is it log n? can someone give me some ideas about estimating complexity of codes?

6条回答
  •  隐瞒了意图╮
    2021-01-14 08:07

    Well, that really depends on what is going on in that loop.

    This loop is linear time, i.e., O(n):

    int sum = 0;
    foreach( int i in SomeCollection )
    {
        sum += i;
    }
    

    However, consider a loop which performs a substring search during each iteration. Now you have to consider the complexity of the string searching algorithm. Your question can't be answered as it stands. You will need to provide a code sample if you want a meaningful answer.

提交回复
热议问题