Here to tell you how to calculate the time complexity of the algorithm (the big O-order)!

About Time Complexity:

In computer science, the time complexity of the algorithm (Time complexity) is a function that describes qualitatively the running time of the algorithm. This is a function of a value representative of an algorithm input string> length. Common large time complexity O symbolic representation, not including the first low-order terms and the coefficients of this function. When used in this manner, the time complexity can be called asymptotic also> input value by examining the size of the case when approaching infinity. For example, if an algorithm is n (must be greater than n0) for input of any size, it requires at most 5n3 + 3n time runs out> Bi, then its asymptotic time complexity is O (n3).
------ Source: https: //zh.wikipedia.org/wiki/%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6

In order to calculate the time complexity, we will estimate the amount normally algorithm operating unit, the operating time of each cell is the same. Thus, the number of total running time and the operation unit in algorithms differ at most a constant factor.
STEP:
1, alternatively a statement by a constant frequency all the items adder
2, results in (1), only the most number of items
3, if the highest order term coefficient is not 1, then the coefficient is removed (or modified 1)

If you do not understand, then read on, the author of the example described in detail with you:
Example 1:

for(int sum=0,i = 0;i < N;i++)  //1、
    for(int j = 0;j <N;j++)     //2、
        sum=sum+i*j;           //3、

We have referred to as a statement 1 to:
1,1+ (N + 1) + N (defined here as a total of two variables, is determined and the intermediate circulation theory is N times, it should actually be the last N + 1 there is determined equal to N times. 1 it is 0 ~ N N + 1, is to increment the loop variable N times total)
2, N + N (N +. 1) + N N (here represented by a first loop into the second cycle, a total of N times, it will define N times, it will judge sentences separately analyzed like the first layer of the same cycle, then a determination count N times the first loop into the second loop so that a total of N times N similar (N + 1) rear N N)
. 3, N N (recycled into the second layer of the innermost layer of sequential statements N layer into the second circulation cycle so the actual total is N times N N times)
then add the numbers:
1+ (N +. 1) + N + N (N +. 1) + N N + N N N * N entries highest retention
so algorithm time complexity of O (N ^ 2)
example 2:

int j = 0;
for(int sum = 0,i = 1;i < N;i = i*2)   //1、
        sum = sum+i*j;             //2、

See this program fragment, you are not the one to get the answer? If you are not careful, your answer must be O(N), right?
To tell you the answer is O(log(N))not the answer, you can try one on behalf of the exact value of N to look into the number of their own taste, then the following analysis under me and everyone together:
1, 1 + log (N) + 1 + log (N)
2, log (N)
actually here to make a judgment, it should look increments loop variable, here it i=i*2is 2 ^ x = N then X = log2 (N) x is the number of times, in computational complexity inside, the general base number is omitted, i.e. direct write log (N), is consistent with other calculation methods explained above.
Example 3:

for(int sum=0,i = 0;i<N;i++)        //1、
    for(int j = 1;j < N;j = j*2)     //2、
        sum=sum+i*j;                //3、

The answer analysis:
1, 1 + 1 + N + N
2.N N + (log (N) +1) + Nlog (N)
3, Nlog (N)
time complexity O (Nlog (N))
If you're serious from seen down here I believe you should be able to understand how to calculate the time complexity, then any program I believe in your own simplistic, they should be able to separate the substantially large O-order.
Then, about the time complexity of the algorithm to describe here.

Published 19 original articles · won praise 3 · Views 3812

Guess you like

Origin blog.csdn.net/weixin_42792088/article/details/100528959