Detailed calculation time complexity

1. Pre-knowledge

Our pre-knowledge before calculating the time complexity is the general term formula and summation formula of the arithmetic sequence and the general term formula and summation formula of the geometric sequence

Arithmetic progression:

General term formula: an=a1+(n-1)d (d is the tolerance)

Sum formula: Sn=n(a1+an)/2

Geometric sequence:

General term formula: an=a1*q^(n-1) (q is the total ratio)

Summation formula: Sn=a1*(1-q^n)/1-q

2. Omit the constant term

To calculate the time complexity, the program segment of the constant term must first be discarded

for example

void fun(int n)
{
    int i=0;
    while(i<=n)
    {
        i++;
    }
}

In this program, int i=0; This sentence is a program of constant items, it will only be executed once, so we only count the number of executions in the while loop

3. Calculation time complexity

Calculating the time complexity is to calculate how many times a certain program needs to be executed, focusing on calculating the program segment with an order. For example, the while loop in the code above has an order. We only calculate the time complexity in the while loop, and it is easy to know that it will be executed. n times, so the time complexity is o(n).

What is the general method for calculating the time complexity

Like this example

void fun(int n)
{
    int i=1;
    while(i<=n)
    {
        i*=2
    }
}

We omit the constant item int i=0, we only count the number of executions in the while loop

We know that calculating the time complexity is the number of times the program is calculated, so here we calculate how many times the while loop is executed in total

Execution times

i

first execution

i=2

second execution

i=4

third execution

i=8

……

……

By analogy: 2, 4, 8, 16,..., it can be found that the value of i constitutes a geometric sequence, where the common ratio is 2, and the nth item an=2*2^(n-1)=>an =2^n

We know that the condition for the end of the while loop execution is i>n. We assume that after k times of execution, i>n, that is, the condition of the while loop is not satisfied. This k value is the scale of the time complexity we require

So according to an we get an inequality, when the value of an>n after k times is executed, that is, when 2^k is greater than n, just get the value of k:

When 2^k=n, k=\log_{2}k

So when k is greater than \log_{2}k, the while loop ends, and the k value is the time complexity scale we require, so the time complexity of this example is O()

4. Case

(1) Find the time complexity of the following program segment

count=0

for(k=1;k<=n;k*=2) ①

for(j=1;j<=n;j++) ②

count++;

First calculate the number of executions of ①

It can be found that k constitutes a geometric sequence, an=2^(n-1), when k=n, it is executed \log_{2}n-1once, that is to say, the time complexity of ① is O( \log_{2}n)

We are judging the time complexity of ②, the number of executions in ② has nothing to do with the outer loop, in order: 1, 2, 3, 4...n, so we can find that it forms an arithmetic sequence, the tolerance is 1, an=n , when the loop ends after n times, the complexity is O(n)

The number of times we calculate the execution of the program is only related to the number of executions of the inner loop, that is, the number of executions of count++. The number of times we calculate the number of executions of count++ is to calculate the number of executions of the entire program, which is the time complexity of this program segment.

It can be found that the number of executions of count++ is determined by the inner loop and the outer loop, that is, the result of multiplying them is the number of executions of count++. We said above that the complexity of the outer loop is , while the inner loop executes n times each \log_{2}ntime , so the total time complexity is n* \log_{2}n, which is O(n \log_{2}n)

The inner loop of this program segment has nothing to do with the outer loop, so the outer loop is executed once, and the inner loop will be executed n times. When the inner loop is restricted by the outer loop, the time complexity needs to be calculated separately.

(2)

int i=0,sum=0;
while(sum<n)		sum+=++i;

The size of the sum is: 0+1, 0+1+2, 0+1+2+3, 0+1+2+3+4...i, and so on, it can be found that an arithmetic sequence is formed, Sn= i(i+1)/2, so the number of executions satisfies (1+k)*k/2<n, that is, k= n^{1/2}times of execution (calculated by the root-finding formula here, the scale of the answer is n^{1/2}), the loop ends, So the time complexity of this program segment is O( n^{1/2})

Guess you like

Origin blog.csdn.net/qq_52905520/article/details/130507446