Python algorithm representation conceptual literacy tutorial, python algorithm literacy tutorial

Source: Internet
Author: User

Python algorithm representation conceptual literacy tutorial, python algorithm literacy tutorial

This article explains the concept of python algorithm representation for your reference. The specific content is as follows:

Constant order O (1)

A constant, also known as a definite number, is a constant with a constant value. The opposite is a variable.

Why is the time complexity of the following algorithm not O (3), but O (1 ).

Int sum = 0, n = 100;/* Run Once */sum = (1 + n) * n/2;/* Run Once */printf ("% d ", sum);/* Rows */

The number of times the algorithm runs is f (n) = 3. According to our derivation of the method of large order O, the first step is to change the constant item 3 to 1. When the highest-order item is retained, it is found that it does not have the highest-order item at all, so the time complexity of this algorithm is O (1 ).

In addition, let's imagine if the statement sum = (1 + n) * n/2 in this algorithm has 10 sentences:

Int sum = 0, n = 100;/* execute 1 time */sum = (1 + n) * n/2; /* Run 1st times */sum = (1 + n) * n/2;/* Run 2nd times */sum = (1 + n) * n/2; /* Run 3rd Times */sum = (1 + n) * n/2;/* Run 4th times */sum = (1 + n) * n/2; /* Run 5th times */sum = (1 + n) * n/2;/* Run 6th times */sum = (1 + n) * n/2; /* Run 7th times */sum = (1 + n) * n/2;/* Run 8th times */sum = (1 + n) * n/2; /* execute 9th times */sum = (1 + n) * n/2;/* execute 10th times */printf ("% d", sum ); /* Run Once */

In fact, no matter how much n is, the above two sections of code are the difference between 3 and 12 executions. This kind of algorithm is independent of the problem size (n). The algorithm with a constant execution time is called the time complexity with O (1) and the constant order.

Note: No matter how much this constant is, we can record it as O (1) instead of any other number, such as O (3) and O (12, this is a common mistake for beginners.

Derivation of Large O-Order Method

1. Replace all addition constants in the running time with constant 1

2. In the modified number of running functions, only the highest-order items are retained.

3. If the highest-order item exists and is not 1, remove the constant multiplied by this item.

Logarithm level O (log2n)

Logarithm

If the x power of a is equal to N (a> 0, and a is not equal to 1), then number x is called the logarithm of N (logarithm) based on ), as x = logaN ,. Here, a is the base number of the logarithm, and N is the real number.
5 ^ 2 = 25, recorded as 2 = log5 25
Logarithm is an operation that is opposite to exponent. For example

① 3 ^ 2 = 9 <=> 2 = log <3> 9;

② 4 ^ (3/2) = 8 <=> 3/2 = log <4> 8;

③ 10 ^ n = 35 <=> n = lg35. For ease of use, people gradually write the base-10 common Logarithm As lgN

Logarithm level

Int count = 1; while (count <n) {count = count * 2;/* sequence of program steps with time complexity of O (1 */}

Since each count multiplied by 2, it is closer to n.

That is to say, the number of 2 after multiplication is greater than n, the loop will exit.

Returns x = log2n from 2 ^ x = n. Therefore, the time complexity of this loop is O (logn ).

Linear order O (n) 

Execution time increases proportionally as the problem grows

data = [ 8,3,67,77,78,22,6,3,88,21,2]find_num = 22for i in data:  if i == 22:    print("find",find_num,i )

Linear logarithm order O (nlog2n)

Square level O (n ^ 2)

for i in range(100):   for k in range(100):    print(i,k)

Cubic order O (n ^ 3)
K to the power of O (n ^ k ),
Exponential order O (2 ^ n ).

As the problem scale n increases, the time complexity increases and the algorithm execution efficiency decreases.

 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.