What is the complexity of time?

Source: Internet
Author: User

What is the complexity of time?

As a slag in the learning path, was baffled by a time complexity problem, then I thought about what is the time complexity. Although learning the algorithm in school courses, but carefully think about the complexity of time is really not understand. Then re-learn, write down some of their own understanding.

1. Complexity of Time

Refers to the time complexity, the first time to think of is the algorithm, simply said, the algorithm is your solution to the problem, and you use this method to solve the problem of the number of statements executed, called the sentence frequency or time frequency, recorded as T (N).

So the question is, why should we introduce these concepts? Because we want to execute an algorithm takes time, this time theoretically can be obtained, but to get this time must be on the machine test, but is it necessary? What we need to know is which algorithm takes a lot of time, which algorithm takes less time, so it can. Moreover, the time of the algorithm is proportional to the number of executions of the statement, that is, the more the statement executes, the more time it takes. That's why we're introducing concepts.

In the above mentioned time frequency t (n), n refers to the size of the algorithm, n constant change, T (n) will continue to change, and what are the laws of these changes? So we introduced the concept of time complexity.

What is the complexity of time, in the algorithm, a function has n basic operation repeated execution, denoted by T (n), there is now an auxiliary function f (n), so that when n approaches Infinity, T (n)/f (n) is the limit value is not equal to zero constant, it is said F (n) is the same order of magnitude of t (N) function. As T (n) =o (f (n)), called O (f (n)) is the progressive time complexity of the algorithm, which is referred to as the complexity of time. In layman's words, the so-called time complexity is finding a function f (n) of the same curve type, which indicates the trend of the algorithm when N is constantly getting larger. When the input n gradually increases, the limit of time complexity is called the asymptotic time complexity of the algorithm.

We use the large O notation to represent time complexity, which is the time complexity of an algorithm. Big O means that there is an upper bound but not an upper boundary.

"Big O notation": The basic parameter used in this description is N, the size of the problem instance, the function of expressing complexity or running time as N. Here the "O" denotes the magnitude (order), for example "binary retrieval is O (Logn)", that is, it needs "to retrieve an array of size n by logn steps" notation O (f (n)) indicates that when n increases, the run time will grow at a rate proportional to f (n).

Time complexity is useful for analyzing and roughly comparing algorithms, but the real situation can be caused by some other factors. For example, an O (N2) algorithm with a low additional cost may run faster than a high-cost O (NLOGN) algorithm in the case of n smaller. However, when n becomes larger, the algorithms for slower-rising functions run faster than in comparison.

I quoted some professional definitions above, which may not be too good to understand, and the following will write some frequently occurring algorithmic time complexity and some examples to explain.

2. Examples of time complexity for simple algorithms

List the time complexity of some simple examples.

The O (1) algorithm is a number of algorithms that are constant in number of operations. For example:

Temp=a;a=b;b=temp;

The above statement has a total of three operations, the frequency of a single operation is 1, even if he has thousands of operations, but also a large constant, this class of time complexity is O (1).

The O (n) algorithm is a number of linear algorithms. For example:

sum=0;

for (i=0;i<n;i++)

sum++;

The first line in the above code is 1, the second line is N, the third line frequency is n, so f (n) =n+n+1=2n+1. So the complexity of Time O (n). In this class of algorithms, the number of operations and n is proportional to linear growth.

O (LOGN) an algorithm that, if it can remove half of the data elements, such as binary retrieval, at each step, usually takes O (logn) time. Give me a chestnut:

int i=1;

while (i<=n)

i=i*2;

The frequency of the above code is f (n), then: 2 f (n) <=n;f (n) <=log?n, whichever is the maximum f (n) = Log?n, so t (n) =o (log?n).

O (n2) (n in the case of K-side) The most common is the usual array to sort the various simple algorithms are O (N2), such as the direct insertion of sorting algorithm.

The algorithm, like the multiplication of matrices, is O (N3).

To give a simple chestnut:

sum=0;

for (i=0;i<n;i++)

for (j=0;j<n;j++)

sum++;

First line frequency 1, second row n, third row N2, fourth row n2,t (n) =2n2+n+1 =o (n2)

O (2 of the n-th square) For example, an algorithm that asks for all subsets of a set of n elements

O (n!) For example, a fully arranged algorithm with n elements

The complexity of time is more complicated by the N-Greater algorithm: Constant order O (1), Logarithmic order O (LOGN), linear order O (n), linear logarithmic order O (NLOGN), square order O (n2), Cubic O (n3) 、...... K-Order O (k-th square), exponential order O (2 n-th).

Now that the complex sort is mentioned, we must say a few more words. We also need to distinguish between the worst-case behavior and the expected behavior of the algorithm. For example, a quick sort, the worst-case run time is O (N2), but the expected time is O (Nlogn). But we can avoid worst-case scenarios by some means, so a well-designed quick sort can run at the desired time.

Finally, let me mention the situation of the index. The exponential algorithm is generally too complex, so the actual case is not forced to use time complexity as an exponential algorithm unless n is particularly small.

I wrote a lot of the most basic things, but also want to deepen the impression, if there are errors please also point out.

Original Address https://www.cnblogs.com/huangbw/p/7398418.html

What is the complexity of time?

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.