A detailed description of the time and space complexity of the algorithm

Source: Internet
Author: User

First, the algorithm

1. The algorithm is a description of the solution steps of the undetermined problem

2. Measure the algorithm's index:

Time complexity: How much time is required to execute this algorithm, that is, the algorithm calculates the number of basic operations performed

Spatial complexity: How much space this algorithm consumes, that is, the measure of the size of the storage space temporarily occupied by the algorithm while it is running, emphasizing the size of the secondary space (the working unit that operates on the data and the auxiliary unit that stores some calculations), not the space occupied by all the data

3, the same problem can be solved with different algorithms, and the merits and demerits of an algorithm will affect the efficiency of the algorithm and even the program. The purpose of the algorithm analysis is to select the appropriate algorithm for the specific problem. The evaluation of an algorithm is mainly considered from time complexity and space complexity.

Algorithms are often contradictory between time efficiency and space efficiency, and usually we assume that the program runs in large enough memory to explore more time complexity

Second, the complexity of time

Common time complexity are: constant order O (1), Logarithmic order O (log2n), linear order O (n), linear logarithmic order O (nlog2n), square order O (n2), Cubic O (N3), K-Order O (NK), exponential order O (2n). With the increasing of the problem scale N, the complexity of the time is increasing and the efficiency of the algorithm is less.

Calculate time Complexity
    • Removes all addition constants from the run time.
    • Only the highest order is preserved.
    • If the highest order exists and is not 1, the time complexity is obtained by removing the constant multiplied by the highest order.
1. Constant order
int 0 ;       /* Execute once */ = (12;         /* Execute once */   printf ("%d", sum);           /* Execute once */  
2. Logarithmic order
int 1 ;          while (Count < N) {     2;     /* sequence of procedure steps with a time complexity of O (1) */   }  

Since each count is multiplied by 2, it is a point closer to N. That is, the number of 2 multiplied by greater than N will exit the loop. Get x=log2n by 2^x=n. So the time complexity of this cycle is O (log2n).

3. Linear order
int i;          for 0; I < n; i++) {      /* * time complexity is O (1) program step sequence */  }  
4, Square Order
int I, J;          for 0; I < n; i++) {      for0; j < N; j + +          ) {/* * time complexity is O (1) sequence of program steps */       }  }  

  

int I, J;          for 0; I < n; i++) {      for (j = i; J < N; j + +){/   * Note j = i instead of 0*/          *  sequence of program steps with TIME complexity O (1) */      }  }  

Because when i=0, the inner loop executes n times, when i = 1 o'clock, executes n-1 times, ... When I=n-1, it was executed 1 times. So the total number of executions is:

5, cubic Order
int I, J;          for 1; I < n; i++)      for1; J < N; J + +)          for1; J < N; J + +              ) {/* * time complexity is O (1) program step sequence */            }  
Three, space complexity 1, recursion situation
intBINARYSEARCH2 (Const int* PTR,Const intXConst intLeftConst intRight ) {      intMid= (left+right)/2;  while(left<=Right ) {          if(x<Ptr[mid]) {              returnBINARYSEARCH2 (ptr,x,left,mid-1); }          Else if(x>Ptr[mid]) {              returnBINARYSEARCH2 (ptr,x,mid+1, right); }          returnmid; }  }  

Spatial complexity in recursive cases: the recursive depth is n the secondary space size of each recursive, and if the secondary space for each recursive is constant, then the spatial complexity is O (N).

For recursive binary lookups, the recursive depth is log2^n, and the secondary space for each recursive is constant, so the spatial complexity is O (log2n) (2 is the base subscript)

2, non-recursive situation
intBinarySearch1 (Const int* PTR,Const intXConst intLen) {      intleft=0; intright=len-1; intMid= (left+right)/2;  while(left<=Right ) {          if(x<Ptr[mid]) { Right=mid-1; }          Else if(x>Ptr[mid]) { Left=mid+1; }          Else          {              returnmid; }      }      return-1; }  

In this process, the secondary space is a constant level, so the spatial complexity is O (1)

A detailed description of the time and space 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.