Algorithm: new knowledge of time complexity

Source: Internet
Author: User

Definition: 1. Frequency of time: the time spent by an algorithm is proportional to the number of statements executed in the algorithm. In an algorithm, when the number of statements executed is large, it takes more time. The number of statement executions in an algorithm is called the statement frequency or time frequency. As T (n ). For example, if [java] for (int I = 0; I <10; I ++) {// execution statement}, the preceding execution statement will be executed 10 times, then his time frequency T (n) = 10; 2. time Complexity: The pattern presented when the time frequency changes. In ascending order of magnitude, common time Complexities include: constant order O (1), logarithm order O (log2n), linear order O (n), linear logarithm order O (nlog2n ), square order O (n2), cubic order O (n3 ),..., k to the power of O (nk), exponential order O (2n ). As the problem scale n increases, the time complexity increases and the algorithm execution efficiency decreases. For example: [java]: 1. for I: = 1 to 100 do for j: = 1 to 100 do s [I, j]: = 0; execution times: 100*100, time complexity O (1) 2. for I: = 1 to n do for j: = 1 to 200 do s [I, j]: = 0; number of executions n * 200 times, time complexity O (n) 2. for I: = 1 to n do for j: = 1 to n div 2 do s [I, j]: = 0; number of executions n * n/2, time complexity O (n ^ 2) 3. for I: = 1 to n do for j: = 1 to n-1 do for k: = 1 to N-2 do s [I, j, k]: = 0; execution times n * (n-1) * (n-2), time complexity O (n ^ 3) 4. for I: = 1 to n do begin for j: = 1 to n do s [I, j, 0]: = 0; For j: = 1 to n do for k: = 1 to n do s [I, j, k]: = 1; end; execution times n * (n + n * n), time complexity O (n ^ 3) [java] Question not to understand for example. Shouldn't the first example be O (10000? Shouldn't the second example be O (n * n/2)? How does the number in the brackets come from? The answers to O (10000) and O (1) are constants, so O (10000) can be considered as O (1 ). O (n * n/2) and O (n ^ 2) are both square orders, so O (n * n/2) can be considered as O (n ^ 2 ). The execution efficiency of an algorithm on the computer mainly depends on the time complexity of the algorithm, and the influence of constants is not great. When n is very large, the O (10000) algorithm and O (1) algorithm run almost the same time, O (n * n/2) algorithm and O (n ^ 2) Algorithm) but the O (n * n/2) algorithm is much slower than the O (10000) 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.