Algorithmic Analysis | 4th episode (cyclic analysis)

Source: Internet
Author: User

we discussed asymptotic analysis in previous posts, worst, average and best case and asymptotic notation. in this article, we discussed using a simple example to analyze an iterative program.

1) O (1): if a function (or set of statements) does not contain loops, recursively and calls any other non-constant time functions, it is treated as a time complexity of O (1).

Non-recursive and non-circular statement sets

For example , the swap () function has an O (1) time complexity.
Typically, a loop or recursion that runs a constant number of times is also considered O (1).

For example, the following loop is O (1).

// Here, c is a   constant    for (int1; I <= C; i++) {          //  some O (1) Expressions   }

2) O (n): time if the loop variable increments/decrements a constant, the complexity of the loop is considered O (n). For example, the following function has a time complexity of O (n).

// Here- c is a positive integer   constant    for (int1; i <= n; i + = c)          {//  some O (1) Expressions   }     for (int0; I-= c)        {//  some O (1) Expressions   }

3) O (n C ) : The time complexity of a nested loop is equal to the number of executions of the inner statement.

For example, the following sample loops have O (n 2 ) Complexity of Time

   for(inti =1; I <=n; i + =c) { for(intj =1; J <=n; J + =c) {//some O (1) Expressions       }   }    for(inti = n; i >0; i + =c) { for(intj = i+1; J <=n; J + =c) {//some O (1) Expressions}

For example, Select Sort and insert sort with O (n 2) time complexity.
4) O (LOGN) time if the loop variable is split/multiplied by a constant amount, the complexity of the loop is treated as O (log n).

 for (int1; I <=n; i *= c) {       //  some O (1) Expressions   }     for (int0; I/= c) {       //  some O (1) expressions    }

For example , binary search (reference iteration implementation) has O (log n) time complexity.
5) O (LOGLOGN) time if the loop variable decreases/increases exponentially, the complexity of the loop is treated as O (LOGLOGN).

// Here c is a constant greater than 1       for (int2; I <=n; i = pow (i, c)) {        //  some O (1) Expressions    }   //Here aresqrt or cuberoot or any other constant rootfor    (in T0; i = Fun (i)) {        //  some O (1) Expressions   }

See more explanations for this.


How do you combine the time complexity of a continuous loop?


When there is a continuous loop, we calculate the time complexity as the sum of the time complexity of a single cycle.

    for (int1; I <=m; i + = c)          {//  some O (1) Expressions   }     for (int1; I <=n; i + = c)        {//  some O (1) Expressions   }   is an O (m+N)   is O (n).    

How to calculate the complexity of time when there are many If,else statements in the inner loop ?


as discussed here, the worst time complexity is most useful between the best, the average, and the worst.

so we need to consider the worst case scenario. We evaluate the case where the value in the If-else condition causes the maximum number of statements to be executed.

For example, considering a linear search function , we consider the case where the element exists at the end or does not exist at all.

When the code is too complex to consider all the if-else cases, we can get the upper limit by ignoring else and other complex control statements.


How do you calculate the time complexity of a recursive function?


the time complexity of recursive functions can be written as a mathematical recursive relationship. in order to calculate the complexity of time, we must know how to solve the loop.

We will soon discuss recursive solutions in a separate article.

Algorithmic Analysis | 4th episode (cyclic analysis)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.