The time complexity calculation of the common sorting algorithm

Source: Internet
Author: User

This blog post non-blogger original, the department through the degree of Niang collection and collation, if there is similar, please contact Bo Master, Chase Plus reprint source. At the same time Bo Master level and understanding is limited, if there is any deviation please the general Bo friends designated.

Learn to communicate qq:792911374

Complexity of Time

The same problem can be solved by different algorithms, and the quality of an algorithm will affect the efficiency of the algorithm and even the program. The time overhead of an algorithm is denoted by T (n), where n indicates the number of times the basic operating module of the algorithm has been repeatedly executed. The time complexity of the algorithm is recorded as T (N) =o (f (n)), with the increase of n, the growth rate of the algorithm execution time is proportional to the growth rate of f (n), so the smaller the F (n), the lower the time complexity of the algorithm, the higher the efficiency of the algorithm. time complexity is often expressed in large O notation. This notation called "Large O-order" notation is used to represent the time complexity of the algorithm in uppercase O.

The algorithm's time complexity (large o-order) is calculated as:

1. Replace all the addition constants in the run time with constant 1.
2. In the modified run Count function, only high-order items are retained.
3. If the highest order exists and is not 1, the constant multiplied by the item is removed.

That is, when n increases to a certain value, the most significant influence on time complexity is the highest term of the power of N, and the other constants and low power are negligible.

For example:

When calculating the complexity of the time, the basic operation of the algorithm is first found, and then the execution times are determined according to the corresponding statements. For example:

1 intn =100000;//executed 1 times.2  for(inti =0; I < n; i++) {//executed n+1 times.3    for(intj =0; J < N; J + +)//performed n (n+1) times4   {5printf"i =%d, j =%d", I, J);//executed n*n times.6   }7 }8 9  for(inti =0; I < n; i++) {//executed n+1 times.Tenprintf"i =%d", i);//executed n times. One } A  -printf" Done");//executed 1 times.

Follow the steps above to derive the "large O-order" first step: "Replace all the addition constants in the runtime with constant 1", then the above calculation becomes:
Total number of executions = 2n^2 + 3n + 1;

The second step: "In the modified run Count function, only the highest order", where the highest order is n two times
So the equation becomes:
Total number of executions = 2n^2;

Step three: "If the highest order exists and is not 1, then the constant multiplied by this item is removed", where n is two times not 1 so
To remove the multiplication of this item, the constant formula becomes:
Total number of executions = n^2;

So, in the end, we get the algorithm time complexity of the code above is expressed as: O (n^2);

In order of magnitude increments, common time complexity is:
Constant order O (1), Logarithmic order O (log2n), linear order O (n)
Linear logarithmic order O (nlog2n), square order O (n^2), Cubic O (n^3) ..., K-Order O (n^k), exponential order O (2^n), with increasing problem size n,
The complexity of the time is increasing, and the efficiency of the algorithm is lower.

For example, the time complexity of nesting A For loop is: O (n), the second layer for loop is O (n^2), the algorithm time complexity of the binary is: O (logn), if a for loop sets a binary, then the time complexity is O (NLOGN);

The time complexity calculation of the common sorting algorithm

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.