Time complexity of the algorithm

Source: Internet
Author: User

Why is algorithmic analysis needed?

    • The resources required to predict the algorithm
      • Calculation time (CPU consumption)
      • Memory space (RAM consumption)
      • Communication time (bandwidth consumption)
    • Predict the run time of the algorithm
      • The number of basic operations performed at the given input scale.
      • or algorithmic complexity (algorithm complexity)

How to measure the complexity of an algorithm?

    • Memory
    • Times (Time)
    • Quantity of instructions (number of Steps)
    • Number of specific operations
      • Number of disk accesses
      • Number of network packets
    • Progressive complexity (Asymptotic complexity)

What is the running time of the algorithm related?

    • Depends on the input data. (for example, if the data is already sequenced, the time consumption may be reduced.) )
    • Depends on the size of the input data. (Example: 6 and 6 * 109)
    • Depends on the maximum run time. (Because the maximum run time is a commitment to the user.) )

Types of algorithmic Analysis:

    • Worst case scenario (worst cases): Maximum run time for any input scale. (usually)
    • Average (Average case): Expected run time for any input scale. (sometimes)
    • Best case: Usually best cases do not appear. (Bogus)

For example, in order to search for a specified value in a list of length n, the

    • Worst case scenario: N-Times comparison
    • Average situation: N/2 times Comparison
    • Best case: 1 Comparisons

In practice, we generally only consider the worst-case operation of the algorithm, that is, for any input of size n, the maximum running time of the algorithm. The reasons for this are:

    1. The worst-case run time for an algorithm is an upper bound (Upper Bound) running time under any input.
    2. For some algorithms, the worst-case scenario occurs more frequently.
    3. Generally speaking, the average situation is usually as bad as the worst.

Algorithm analysis to maintain bigger picture (Big idea), its basic ideas:

    1. Ignore those constants that depend on the machine.
    2. Focus on the growth trend of uptime.

For example, the trend of t (n) = 73n3 + 29n3 + 8888 is equivalent to T (N) =θ (n3).

The asymptotic notation (asymptotic Notation) usually has O, Θ, and Ω notation. The Θ notation progressively gives the upper and lower bounds of a function, using the O notation when there is only an asymptotic upper bound, and using the Ω notation when there is only the asymptotic lower bound. Although the technical Θ notation is more accurate, it is usually indicated by the O notation.

Use the O notation (Big O Notation) to indicate the upper bound of the worst-case operation. For example

    • The linear complexity O (n) indicates that each element is to be processed once.
    • The square Complexity O (n2) indicates that each element is to be processed n times.

The method for calculating the progressive run time of a code block has the following steps:

    1. Determine the constituent steps that determine the algorithm run time.
    2. Locate the code that performs the step, labeled 1.
    3. View the next line of code that is labeled 1. If the next line of code is a loop, Mark 1 is modified to 1 time times the number of cycles 1 * N. If more than one nested loop is included, the multiplier will continue to be calculated, for example, 1 * n * M.
    4. The maximum value to be found is the maximum value of the run time, which is the upper bound of the algorithm complexity description.

Case 1:

Factorial (factorial), given the size n, the number of basic steps performed by the algorithm is n, so the algorithm complexity is O (n).

decimal Factorial (int  N)    {      if0)        return1 ;       Else        return 1 );    }

Case 2:

n the size of an array of arrays, the worst case is to compare n times to get the maximum value, so the algorithm complexity is O (n).

1 intFindmaxelement (int[] array)2     {3       intmax = array[0];4        for(inti =0; I < array. Length; i++)5       {6         if(Array[i] >max)7         {8Max =Array[i];9         }Ten       } One       returnMax; A}

Case 3:n the size of an array of arrays, the number of executions of the basic step is approximately n (n-1)/2, so the algorithm complexity is O (n2).

 long  findinversions (int  [] array) { long  inversions = 0  ;  for  (int  i = 0 ; I < array. Length; I++)  for  ( int  j = i + 1 ; J < Array. Length;  J++)  if  (Array[i] > Array[j]) inversions  ++;     return   inversions; }

Case 4: Given the size n and M, the number of executions of the basic step is n*m, so the algorithm complexity is O (n2).

Long SUMMN (intint  m)    {      long0;        for (int0; x < n;          for (int0; y < m; y++)           + = x * y      ; return sum;    }

Case 5: Given the size n, the number of executions of the basic step is approximately n*n*n, so the algorithm complexity is O (n3).

 decimal  Sum3 (int   N) { decimal  sum = 0  ;  for  (int  a = 0 ; a < n; A++)  for  ( int  b = 0 ; b < n; B++)  for  (i NT  c = 0 ; c < n;      C++) sum  + = A * b * C;     return   sum; }

Case 6: The basic operation of inserting a sort is to insert a data into the ordered data that is already sorted, thus obtaining a new ordered data. The algorithm is suitable for ordering small amounts of data, and the time complexity is O (n2).

Private Static voidInsertionsortinplace (int[] unsorted) {       for(inti =1; I < unsorted. Length; i++)      {        if(Unsorted[i-1] >Unsorted[i]) {          intKey =Unsorted[i]; intj =i;  while(J >0&& Unsorted[j-1] >key) {Unsorted[j]= Unsorted[j-1]; J--; } Unsorted[j]=key; }      }    }

Reference: http://www.cnblogs.com/gaochundong/p/complexity_of_algorithms.html

Time complexity of the 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.