Python algorithm Basics

Source: Internet
Author: User

Python algorithm Basics

1. recursion:

Recursion: a recursive algorithm is a process of directly or indirectly calling its own algorithms. In computer programming, recursive algorithms are very effective in solving a large variety of problems, it makes the description of an algorithm concise and easy to understand;
Features:

(1) recursion is to call itself in a process or function;
(2) When using a recursive policy, there must be a clear recursive termination condition, called the recursive exit;
(3) recursive algorithms are usually simple in solving problems, but they are less efficient in solving problems. Therefore, designing programs using recursive algorithms is generally not recommended;
(4) During the recursive call process, the system opens a stack for storing the return points and local volumes of each layer. Excessive recursion times may cause stack overflow, therefore, recursive algorithms are generally not recommended for programming;
Requirements:
There are generally three requirements for recurrence in recursive algorithms:
(1) Each call is scaled down (usually halved );
(2) There is a close relationship between two adjacent duplicates. The previous one should be prepared for the next one (the previous output is usually used as the next input );
(3) When the problem scale is extremely small, it must be answered directly without recursive calls, therefore, each recursive call is conditional (based on the fact that the scale does not reach the size of the direct answer). recursive calls without a component will become an endless loop and cannot end normally;
Example:

2. Recursive Implementation of the sequence of two consecutive orders (each number is always the same as the values of the first two sets ):

3. Binary Search Based on algorithms:

No 1 will be found. make the following corrections:

4. Two-dimensional array based on algorithms:

1> how to generate a list:

      

2> Generate a two-dimensional array:

      

Eg: Flip a two-dimensional array 90 degrees:
Self-made:

Refer to the instructor code:

            

5. algorithm-based Bubble Sorting:

    

6. Time Complexity:

1> time frequency:

The time it takes for an algorithm to be executed cannot be calculated theoretically. It must be tested on the computer before we can understand it. When we do not have to perform a computer test on every algorithm, you only need to know which algorithm takes more time and which algorithm takes less time. In addition, the time spent by an algorithm is proportional to the number of statements executed in the algorithm, in an algorithm, when statements are executed more frequently, it takes more time. The number of statements executed in an algorithm is called the statement frequency or time frequency, which is recorded as T (n );
2> time complexity:
In the Time Frequency just mentioned, n is called the scale of the problem. When n is constantly changing, T (n) will also change; however, sometimes we want to specify the pattern when it changes. Therefore, we introduce the concept of time complexity. Generally, in the algorithm, the number of repeated executions is a function with a problem scale of n, represented by T (n). If an auxiliary function f (n) exists ), so that when n approaches infinity, the limit value of T (n)/f (n) is a constant not equal to zero, then f (n) is T (n) the same order of magnitude function, recorded as T (n) = O (f (n), called O (f (n) as the progressive time of the Algorithm
Inter-complexity, referred to as time complexity;
3> exponential time:
It refers to the calculation time m (n) required for solving a problem, which grows exponentially according to the input data size n (that is, the number of input data grows linearly, the time it takes will grow exponentially)
4> example:

The time complexity of the first for loop is O (n), and the time complexity of the second for loop is O (n ^ 2 ), the time complexity of the entire algorithm is O (n + n ^ 2) = O (n ^ 2 );

5> constant time:
For an algorithm, if the upper bound of T (n) is irrelevant to the input size, it has a constant time and is recorded as O (1) time. An example is to access a single element in the array, however, finding the smallest element in the unordered array is not because it needs to traverse all elements to find the minimum value, which is a linear time operation, or O (n) time. However, if you know the number of elements in advance and assume that the number remains unchanged, this operation can also be called a constant time;
6> logarithm time:
If the algorithm T (n) = O (log n), it has a logarithm time. common algorithms with a logarithm time include binary tree related operations and binary search; the algorithm for logarithm time is very effective, because the extra computing time required for each input is reduced; recursively cutting the string half and outputting it is a simple example of this class function;
7> Linear Time:
If the time complexity of an algorithm is O (n), it is called linear time, or O (n) time. In informal terms, this means
For a large enough input, the increase in running time is linearly related to the input. For example, the time required for a program to calculate the sum of all elements in the list is proportional to the length of the list;

 

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.