What is the time complexity, how to find the time complexity, to see which one is enough

What is the time complexity

The time complexity is used to facilitate the development of the program is estimated run time

How do we estimate the running time of it, we usually estimate the number arithmetic operating unit of time to represent the program consume , where we default run consumed per unit time of the CPU is the same.

Suppose the algorithm is the problem size n, then the number of the operating unit will use the function f (n) is represented

When n is increased the size of data, the same algorithm execution time of growth and f (n) growth rate, which is referred to as time asymptotic complexity of the algorithm, referred to as the time complexity , denoted by O (f (n))

What is the big O

Here we must talk about the big O, what is it big O, many students say that time when they are aware of the complexity of O (n), O (n ^ 2), but can not tell what is the big O

Introduction explanation given algorithm: Big O is used to indicate the upper bound, the upper bound when using it as a worst case running time of the algorithm, the running time is the upper bound for any data input.

The same introduction algorithm gives an example: Take for insertion sort, insertion sort we say that the time complexity is O (n ^ 2), but the data have been ordered in the case where the time complexity is O (n-) , also for all cases it is input, the worst case is O (n ^ 2) time complexity , so called insertion sort time complexity of O (n ^ 2)

The same Similarly we look at the quick sort, quick sort all know is O (nlogn), but when the data has been ordered, the quick sort time complexity is O (n ^ 2), and strictly from Big O's By definition, the time complexity of quick sort should be O (n ^ 2)

But we still say quicksort is O (nlogn) time complexity, this is a default provision of the industry is in general, not a strict upper bound we say here on behalf of O

Here Insert Picture Description

So here we know all about like

Interview the interviewer will not discuss the definition of O for the time complexity of quick sort of problem, we know the time to discuss complexity refers to the time in the general case complexity just fine .

We have to time complexity of the algorithm have such a concept

The same time is a complicated algorithm of not static, and data input form is still a relationship

Our main concern is data form in the general case.

Interview , he said the time complexity of the algorithm is how much are references to the general

But if the interviewer and our depth study and implement the performance of an algorithm of time we will always think of data use case is not the same time complexity is different, that students should pay attention

How to describe the time complexity

This figure we can see the difference in the time complexity of different algorithms at different input data size.
Here Insert Picture Description

We decided to use those algorithms when the time is not complicated, the lower the better, consider the size of the data, if the data size is small even by O (n ^ 2) algorithm is more appropriate than O (n)

Like the above figure drawing O (5n ^ 2) and O (100n) when n is obviously before the 20 O (5n ^ 2) is superior, the time spent is the least.

Then why are we in when the computational complexity of time to ignore the constant coefficient of it , is to say O (100n) is O (n) time complexity, O (5n ^ 2) is O (n ^ 2) time complexity degree

But also to default O (n) is better than O (n ^ 2) it?

Here also involves the definition of big O

Because the Big O is actually a breakthrough point data of the order and the order data at very large demonstrated time complexity, this point is the constant coefficient can not afford to have a decisive role in point .

20 is the example figure above that point, n-constant as long as the coefficient of greater than 20 has not critical to the.

So we say that the time complexity coefficient is a constant term is omitted, since we are generally the default data size large enough , based on the fact our time of complex algorithms are given a ranking as follows:

O(1)常数阶 < O(logn)对数阶 < O(n)线性阶 < O(n^2)平方阶 < O(n^3)(立方阶) < O(2^n) (指数阶)  

You do not know O (logn)

We usually say that this time complexity of the algorithm is logn, it must be n log base 2 logarithm of it?

In actual fact, can also be based on the number n of the base 10, can also be based on the number n of 20 to the end, but we are unified said logn, which is ignored in base description.

Why you can do it?

As shown below
Here Insert Picture Description

If we have two time complexity algorithms are logarithmic log and log 2 n to a bottom 10 of the logarithm to the base n

So here if we remember our high school math, it should not be interpreted in a number of base 2 of n = 2 logarithm to the base 10 multiplied by the logarithm to the base 10 of the n

That here the logarithm base 2 is a constant 10, and I've told us calculate the time complexity is to ignore the constant coefficient in the above

Abstract look at the number of log n i is the bottom number is equal to log on to the end j is n, so we ignore i, just say that logn, officially because it is a constant logij

So, so difficult to understand why we should ignore the base number

If the time complexity is a complex expression, how do we simplify

Sometimes, we have to calculate the time complexity when found not a simple O (n) or O (n ^ 2), but a complex expression, such as:

O(2*n^2 + 10*n + 1000) 

Here we describe how it is usually the time complexity of the algorithm of it, it is a way to simplify the process

Removing the additive constant term in the run-time (as the constant term and will not increase the number n increases with operation of the computer)

O(2*n^2 + 10*n)

Remove the constant coefficient (we've just talked about in detail the reasons why the constant term can be removed)

O(n^2 + n)

Reserved only the highest order of removing small items in a n (n ^ 2 because the data size is much larger than n), finally reduces to:

O(n^2) 

If this step the students have difficulty understanding, you can also do the extraction operation n, it becomes O(n(n+1)), after omitting the addition constant term and do not become

O(n^2) 

So in the end we say: our algorithm time complexity of the algorithm is O (n ^ 2)

It can be simplified with another idea, when n is greater than 40, this complexity is less than would have beenO(3*n^2)

O(2*n^2 + 10*n + 1000)  < O(3*n^2)

So finally we omit the final constant time complexity coefficient is O (n ^ 2)

To illustrate how to calculate the time complexity

Specifically, we should count how time complexity through a topic, look at

Description Title: identify the same string two strings of n (assuming the same string only two here)

Some students may think that to solve This question is an enumeration traversal solution can be used, the time complexity is O (n ^ 2)

This time complexity is not correct

Here are some students ignore the string comparison of time spent here is not as simple as comparing figures do int type, in addition to the number of traversal n ^ 2 times, string comparisons are still consumes operations m (m is the string of letters length) , the time complexity isO(m*n*n)

Then we think about the other problem-solving ideas

We first row to the n-th string in the dictionary sort order, sorted n string is ordered, meaning that the same string two next to each other

Then traversing over n strings, so to find the same string two

Then we take a look at the time complexity of the algorithm

Quick Sort time complexity is O (nlogn), to consider the length of the string still is m, then the quick sort should have every m-th comparison operation of comparing a character is O (m n- logN)

Then we have to traverse again n this string to find two identical strings, do not forget to traverse the time still to compare strings, so the total time complexity is O(m*n*logn + n*m)

We O(m*n*logn + n*m)simplify operation, the m*nextracted become O(m*n*(logn + 1)),

In the constant term is omitted last time complexity is O(m*n*logn)that we compare the time efficiency O(m*n*logn)is not better than the first method O(m*n*n)is faster yet

Clearly O(m*n*logn)superiorO(m*n*n)

So first find two of the same set of strings to sort strings in a faster way to traverse over direct way than violence enumeration .

Through this example we want to count the time complexity is how to have a preliminary understanding and awareness.

Welcome to the concern of my micro-channel public number: "Code Wandering" front-line Internet technology practitioners, to share their ideas and thinking of the Internet and technology. After concerns reply: "java", "C ++", "python" "Algorithms and Data Structures" and so on keywords can get sorted out my years of learning materials. Push in the job can come to me, the most important Baidu. Tencent, Ali, headlines, etc. can help me

Published 230 original articles · won praise 248 · views 260 000 +

Guess you like

Origin blog.csdn.net/youngyangyang04/article/details/105182447