Lesson six, measurement of algorithm efficiency

Source: Internet
Author: User

one, the common time complexity

Comparison of common time complexity

second, the algorithm analysis

Define an array

The best case of this algorithm is performed once

And the worst is going to happen n times.

Note: In the data structure course, when there is no special description, the time complexity of the algorithm is referred to as the worst time complexity

third, the spatial complexity of the algorithm (space complexity)

(1), definition: s (n) = s (f (n))

n is the problem size of the algorithm

F (n) is a space-use function, with N-related

Note: The method of derivation of time complexity also adapts to space complexity

For example: when the space required for the algorithm is constant, the spatial complexity is s (1)

(2), space complexity calculation exercise

four, time and space algorithm strategy

(1), in most cases, the time complexity of the algorithm is of concern

(2), if necessary, can reduce the complexity of time by adding additional space

(3), similarly, can also reduce the space complexity by increasing the time-consuming algorithm

Examples of time-changing spaces

/*problem: In an array consisting of some numbers in the natural number 1-1000, each number may appear 0 or more times. Design an algorithm to find the most frequently occurring numbers. */#include<iostream>using namespacestd;voidSearchintA[],intLen//time complexity of O (n){    intsp[ +] = {0}; intMax =0;  for(intI=0; i<len; i++) {Sp[a[i]-1]++;//Store the total number of corresponding numbers in order, such as the position where the number of 1 exists sp[0], the number of 2 exists sp[1] position    }         for(intI=0; i< +; i++)    {        if(Max <Sp[i]) {Max=Sp[i]; }    }         for(intI=0; i< +; i++)    {        if(max = =Sp[i]) {cout<< i +1<< Endl;//print out the largest number of numbers (not necessarily one)        }    }}intMainintargcChar*argv[]) {    intA[] = {1,1,3,4,5,6,6,6,3,3}; Search (A,sizeof(a)/sizeof(*a)); return 0;}

V. Summary

(1), generally, the algorithm used in the project, the time complexity does not exceed O (n^3)

(2), algorithm analysis design, focus on the worst case of time complexity

(3) The time complexity of the key care algorithm in the data structure course

(4), Space change time is a common strategy in engineering development

Lesson six, measurement of algorithm efficiency

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.