1-03 The worst time complexity

The worst time complexity

Analysis algorithms, there are several possibilities to consider:

  • Algorithm to complete the work requires a minimum number of basic operations, that is the best time complexity
  • How many basic arithmetic operations take up the job done, that is the worst time complexity
  • How many basic arithmetic operations to complete work on average, that the average time complexity

 

For optimal time complexity, which is of little value, because it does not provide any useful information, which reflects only the most optimistic ideal situation, there is no reference value.

For the worst time complexity, provide a guarantee, show that the algorithm in the basic operation of such a degree will be able to complete the work.

For the average time complexity, it is a comprehensive evaluation of the algorithm, so it is a complete and comprehensive reflection of the nature of the algorithm. On the other hand, this measurement is not guaranteed, not every calculation can be completed within this basic operation, but also for the calculation of the average case, also because the application example of the algorithm may not be uniformly distributed but is difficult to calculate.

Therefore, we focused on the worst-case algorithm, that is the worst time complexity.

 

Time complexity of computing a few basic rules

1. Basic operation, i.e., only the constant term, that its time complexity is O (1)

2. The structure of the sequence, the time complexity is calculated by adding

3. The loop structure, the time complexity is calculated by multiplying

4. A branch structure, take the maximum time complexity

5. Analyzing the efficiency of an algorithm, often only need to focus on the operation amount of the highest degree term, other minor items and the constant term can be ignored

6. In the absence of special note, we analyzed the time of the algorithm complexity refer to the complexity of the worst time

 

for a in range(0,1001):
   for b in range(0,1001):
       c = 1000-a-b
       if a**2+b**2 == c**2:
          print("a,b,c:%d,%d,%d"%(a,b,c)

 

The above algorithm:

  T(n)=n*n*(1+1)=n^2*2

  T(n)=O(n^2)

 

Guess you like

Origin www.cnblogs.com/echo-kid-coding/p/11124419.html