Data structure and Algorithm learning summary 02 The measurement of algorithm efficiency

Source: Internet
Author: User

Efficiency of the algorithm

We measure the efficiency of the algorithm: pre-analysis and estimation methods before the computer program, based on statistical methods to estimate the algorithm

The efficiency of the algorithm is measured by abstraction rather than by precise measurement, ignoring hardware aspects, program compilation optimizations, code loop termination conditions, and variable declarations.

The following function as a general algorithm for efficiency judgment

Example:

Function N=1 2 3 100 After omitting
N+2 3 4 5 102 100
2n+1 3 5 7 201 200
2n+2 4 6 8 202 100
2n^2+2 4 10 20 20002 10000
2n^2+n+2 5 12 23 20102 10000
2n^3+n+2 5 20 59 2000102 1000000

When comparing the efficiency of 2n+1 and n+2, when the value of n is increasing, the constants can be omitted and the judgment of efficiency is not affected;

When comparing the efficiency of 2n+2 and 2n^2+2, when the value of n is increasing, the coefficients of constants and the highest term can be omitted, and the judgment of efficiency is not affected;

When comparing the efficiency of 2n^2+n+2 and 2n^3+n+2, when the value of n is increasing, the constants, except the highest ones, and the highest ones can be omitted, without affecting the judgment of efficiency;

Summary : When judging the efficiency of an algorithm, constants and other minor items in the function can often be omitted, and only the order of the highest item should be concerned .

When the judgment algorithm is not good, it is necessary to make accurate judgments through a large amount of data , and when the data is less, it may be biased.

Complexity of Time

Use uppercase O () to represent the time complexity of the algorithm

By judging the efficiency of the above function, we can understand that the computational time complexity is the same, divided into the following steps:

1. Ignore the input-independent code snippet, if the function is only additive, you can directly replace with the constant 1;

2. If the highest order is present in the altered run function, the minor item can be omitted;

3. If the highest order exists and is not 1, the constant of the highest order is removed;

4. Finally, the results will be collated (removing the constant 1, etc.), is the Big O ().

Example: Find the time complexity of the following code snippet.

n = n + +;                                   Performs 1 function (n);                                   Executes n times for (int i = 0; i < n; i++)                     executes n*n times {      function (n);} for (int i = 0; i < n; i++)                     executes N (n+1)/2 times {for      (int j = i; J < N; j + +)      {            printf ("Girl Gen Eration!!! " );      }}  void function (int value) {for (int i = 0; i < value; i++) {printf ("I love you~");}}   

Adding the above execution times together is 3/2n^2+3/2n+1

After finishing the n^2, the time complexity of getting the code snippet is O (n^2).

Note: O (1) <o (LOGN) <o (n) <o (NLOGN) <o (n^2) <o (n^3) <o (2^n) <o (n!) <o (n^n)

Worst run time: Extract a desired number from an array of n random numbers, with a time complexity of O (1), or O (n),

So the worst run time is a guarantee, and generally we refer to runtime as the worst-case run time.

Complexity of space

In general, we ask for "complexity", which refers to the complexity of time.

In the case of some large storage space, it is possible to change the complexity of time with space complexity.

Example: Judging 21st century of those years is a leap year?

The first method is to judge whether each year is a leap years, write some code

The second method is to create an array, sorted in order from 2000-2,100, and set the array element corresponding to the leap year to 1, not 0

The first way to compare time, the second way to occupy large memory, more space, but save time.

So in some cases we can use the overhead of space in a similar second way in exchange for the overhead of time.

Data structure and Algorithm learning summary 02 The 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.