Time complexity: The basic number of times a function calculates a row
Interview Tip: The interviewer asked the time complexity is not necessarily the worst, generally people will directly answer the worst, in fact, there are best and average.
For example, search for a data x in a linear table of length n.
Worst: Not found, only found at the end
Average: Bad not bad to the middle only to find
Best: Very happy first found, generally must pay attention to, but in the hash of the time will be concerned about
The official saying:
Worst case: Maximum run time for any input scale. (upper bound) Average: The expected run time of any input scale. Best case: Minimum run time of any input scale, usually not at best. (Nether) In practice we usually consider the worst-case scenario of the algorithm. That is, for any input size n, the maximum running time for the algorithm is as follows: The worst-case run time of an algorithm is the upper bound of the run time under any input. For some algorithms, the worst-case scenario occurs more frequently. In general, the average is as bad as the worst. Algorithm analysis to keep bigger picture: ignore those constants. Focus on the growth trend of the uptime and focus on the fastest growing expression in the function.
Describing this time complexity of the station in the mathematician's perspective will always abstract a thing, that this east I think is the progressive representation of the Big O,
For example: F (N) = n^3 + n^2 + N +1000, then focus on N^3->o (n^3)
void Test1 (int N)
{for
(int i = 0; i < n; + + i)
{for
(int j = 0; J < N; + + j)
{
//). .
}
}
for (int k = 0; k < 2 * N; + + k)
{
//...
}
int count = ten;
while (count--)
{
//...
}
}
The time complexity of Test1 is: O (n^2)
void Test2 (int N, int M)
{for
(int i = 0; i < M; ++i)
{
} for
(int k = 0; k < N; ++k)
{
//...
}
} void Test3 (int N, int M)
{for
(int i = 0; i < M; ++i)
{for
(int j = 0; j < N; ++j)
{
//...}}
The time complexity of TEST2 is: O (m+n)
void Test3 (int N, int M)
{for
(int i = 0; i < M; ++i)
{for
(int j = 0; j < N; ++j)
{
// ...
}
}
}
The time complexity of TEST3 is: O (m*n)
Here are some common algorithms for the time Complexity: bubbling: (comparison) arithmetic progression: (n-1) + (n-2) +.......+1 = N (1+ (n-1))/2 = 1/2n^2 approximately equals O (n^2) binary lookup: (premise: Ordered) is equivalent to a search binary tree (left child Biggen the right child is smaller than the root), each discharge half of the assumption that the height of the tree h node has n 2^h-1 = n Two minutes to find the time complexity equivalent to the height of the search binary tree ... Isn't it shocking to expand here if not binary search, three points, four points, five points. Isn't that a multi-pronged tree? haha binary search time complexity: O (log2n) deformation is: O (logmn) (M is a few forks)
Origami method to understand binary search
Time complexity calculation of recursive algorithm
The time complexity of the recursive algorithm is the number of recursive which comes times * per recursive count.
Spatial complexity is calculated in the same way as time complexity, and the progressive representation of large o is used. --(Number of objects) to note is the spatial complexity of the recursive algorithm, if the recursive depth is n each recursive space size of 1, then the spatial complexity of O (N).
The computation of time complexity & spatial complexity in Fibonacci sequence learning
Long long Fibonacci1 (int n) {return n < 2? N:fibonacci (n-1) + Fibonacci (n-2)} Fibonacci Recursive algorithm time complexity: O (2 ^n) space complexity is: O (N) (please draw yourself to think about the time complexity and the complexity of space here.) )
Implementing the Fibonacci Sequence Upgrade 1: Requires a time complexity of O (N). Upgrade 2: Requires a time complexity of O (N), the spatial complexity of O (1).
Fibonacci Optimization Long Long Fibonacci2 (int n) {Long long * Fibarray = new long long[n+1]; Fibarray[0] = 0; FIBARRAY[1] = 1;
for (int i = 2; I <= n; ++i) {Fibarray[i] = fibarray[i-1] + fibarray [i-2]; }
Long long ret = fibarray[n]; Delete[] Fibarray;
return ret; } Long long Fibonacci3 (int n) {long long fibarray[3] = {0, 1, n};
for (int i = 2; I <= n; ++i) {fibarray[2] = Fibarray [1] + fibarray[0]; FIBARRAY[0] = Fibarray [1]; FIBARRAY[1] = Fibarray [2]; }
return Fibarray [2]; }