Python Learning-09 (Find, sort, and talk about data structures)

Source: Internet
Author: User

Find the method:

Method of sorting:

A simple data structure:

First, calculate the basis 1.1, what is the algorithm:

The algorithm (algorithm) is an accurate and complete description of the solution, a series of clear instructions to solve the problem, and the algorithm represents a systematic method to describe the strategy of solving the problem. In other words, the input of a certain specification can be obtained in a limited time with the required output. If an algorithm is defective, or is not suitable for a problem, executing the algorithm will not solve the problem. Different algorithms may use different time, space, or efficiency to accomplish the same task. The merits and demerits of an algorithm can be measured by the complexity of space and time.

Simply put, the algorithm is a computational process, the solution to the problem.

An algorithm should have the following seven important characteristics:

① (finiteness): The poor nature of the algorithm means that the algorithm must be able to terminate after a finite number of steps have been performed;

② Certainty (definiteness): Each step of the algorithm must have a definite definition;

③ input: An algorithm has 0 or more inputs to characterize the initial situation of the operands, so-called 0 input refers to the algorithm itself set the initial conditions;

④ output: An algorithm has one or more outputs to reflect the results of processing the input data. The algorithm without output is meaningless;

⑤ feasibility (Effectiveness): Any calculation steps performed in the algorithm can be decomposed into basic executable steps, that is, each calculation step can be completed within a finite time (also known as validity);

⑥ efficiency (High efficiency): The execution speed is fast, consumes less resources;

⑦ Robustness (robustness): responds correctly to data.

1.2. Complexity of Time

Definition: If the scale of a problem is n, the time required for an algorithm to solve this problem is T (n), which is a function of n (n) called the "time complexity" of the algorithm.

When the input n gradually increases, the limit of time complexity is called the asymptotic time complexity of the algorithm.

In addition, a problem itself has its complexity, if the complexity of an algorithm reaches the lower bounds of the complexity of the problem, it is called such an algorithm is the best algorithm.

In computer science, the time complexity of the algorithm is a function, it quantitatively describes the algorithm's running time, time complexity commonly used large o symbol (Big O notation) is used to describe the function of progressive behavior of mathematical symbols. Rather, it is using another (usually simpler) function to describe the asymptotic upper bounds of the order of magnitude of a function. In mathematics, it is generally used to characterize the remainder of the truncated infinite series, especially the asymptotic series; in computer science, it is very useful in analyzing the complexity of the algorithm. ), when using this approach, the time complexity can be called asymptotic, and it examines when the input value is approaching infinity.

Big O, in short, can be thought of as the meaning of "order of" (approximately).

Infinity Asymptotic
The large o symbol is useful when analyzing the efficiency of an algorithm. For example, the time taken to solve a problem of size n (or the number of steps required) can be evaluated: T (n) = 4n^2-2n + 2.
When n increases, n^2; Items will start to dominate, while others can be ignored-for example: when n = 500,4n^2; The item is 1000 times times larger than the 2n item, so in most cases, omitting the effect of the latter on the value of the expression will be negligible.

Calculation of time complexity:

1). The time it takes for an algorithm to execute is theoretically impossible to figure out, and must be run on the machine to be tested. But we can not and do not need to test each algorithm, just know which algorithm spends more time, which algorithm spends less time on it. And the time that an algorithm spends is proportional to the number of executions of the statement in the algorithm, which algorithm takes more time than the number of statements executed.

the number of times a statement is executed in an algorithm is called a statement frequency or time frequency. Note as T (n).

2. In general, the number of times the basic operation of the algorithm is repeated is a function f (n) of module N, so the time complexity of the algorithm is recorded: T (n) =o (f (n)). As the module n increases, the growth rate of the algorithm execution is proportional to the growth rate of f (n), so the smaller the F (n), the lower the complexity of the algorithm, the higher the efficiency of the algorithm.

In the calculation of time complexity, first find the basic operation of the algorithm, and then according to the corresponding statements to determine its execution times, and then find T (n) The same order of magnitude (its same order of magnitude has the following: 1,LOG2N, N, nlog2n, n Squared, N of three square, 2 N-Square, n! ), when found, f (n) = The order of magnitude, if T (n)/f (n) the limit can be obtained a constant C, the time complexity of T (n) =o (f (n)).

3. Common time complexity

In order of magnitude increments, common time complexity is:

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).

which

1.O (n), O (N2), Cubic O (n3),..., K-order O (NK) is a polynomial order time complexity, known as the first-order time complexity, second-level time complexity ....

2.O (2n), exponential order time complexity, this kind of not practical

3. Logarithmic order O (log2n), Linear logarithmic order O (nlog2n), in addition to the constant order, the efficiency of the most

Example: algorithm:

1    for(i=1;i<=n;++i)2 3   {4 5       for(j=1;j<=n;++j)6 7      {8 9c[I [J]=0; This step belongs to the number of basic operations performed: N^2Ten  One            for(k=1;k<=n;++k) A  -c[I [j]+=a[i] [k]*b[K] [j]; This step belongs to the number of basic operations performed: N^3 -  the      } -  -}

Then there is t (n) = n^2+n^3, according to the same order of magnitude in parentheses, we can determine that n^3 is the same order of magnitude of T (N)

Then there is f (n) = n^3, then the constant C can be obtained by the limit of T (n)/f (n)

The time complexity of the algorithm: T (n) =o (n^3)

The complexity of Time is: O (1)

1 temp=i; 2 i=J; 3 j=temp;                 

The frequency of the above three individual statements is 1, and the execution time of the program segment is a constant independent of the problem size n. The time complexity of the algorithm is the constant order, which is recorded as T (N) =o (1). If the execution time of the algorithm does not grow with the increase of the problem size n, even if there are thousands of statements in the algorithm, the execution time is only a large constant. The time complexity of such an algorithm is O (1).

Time complexity: O (n2)

Exchange of contents of I and J

1      sum=0;                 (once)2for      (i=1;i<=n;i++)       (n times)3         for (j=1;j<=n;j++) (n^2 times)4          sum++;       (n^2 times)

Solution: T (n) =2n^2+n+1 =o (n^2)

Example

1    for (i=1;i<n;i++) 2     {3         y=y+1;         ①  4 for          (j=0;j<= (2*n); j + +)   5            x + +;        ②     6     }        

Solution: The frequency of statement 1 is n-1

The frequency of Statement 2 is (n-1) * (2n+1) =2n^2-n-1

F (n) =2n^2-n-1+ (n-1) =2n^2-2

The program has a time complexity of T (n) =o (n^2).

The complexity of Time is: O (n)

1     a=0; 2     B=1;                      ①3 for      (i=1;i<=n;i++) ②4    5        s    =a+B;     ③6        b=7        a=s; ⑤8     }

Solution: Frequency of Statement 1:2,

Frequency of statement 2: N,

Frequency of Statement 3: N-1,

Frequency of Statement 4: n-1,

Frequency of Statement 5: N-1,

T (n) =2+n+3 (n-1) =4n-1=o (n).

Time complexity: O (LOG2N)

1      I=1;       ①2while      (i<=N)3        I=i*2;②

Solution: The frequency of statement 1 is 1,

The frequency of setting statement 2 is f (n), then: 2^f (N) <=n;f (n) <=log2n

The maximum value f (n) = log2n,

T (n) =o (log2n)

Time complexity: O (n3)

 1  for  (I=0;i<n;i++"  2   { 3  for  (J=0;j<i;j++4   { 5< /span> for  (K=0;k<j;k++)  6  x=x+2;  7  }  8      }

Solution: When the i=m, J=k, the number of times the inner loop is k when I=m, J can take 0,1,..., m-1, so here the most internal cycle of 0+1+...+m-1= (m-1) m/2 times So, I from 0 to N, then the cycle has been carried out: 0+ (1-1) *1/2+ ... + (n-1) n/2=n (n+1) (n-1)/6 So time complexity is O (n^3).

We should also distinguish between the worst-case behavior of the algorithm and the expected behavior. For example, the worst-case run time for fast sorting is O (n^2), but the expected time is O (Nlogn). By carefully selecting the benchmark value each time, it is possible to reduce the probability of the squared condition (i.e. O (n^2)) to almost equal to 0. In practice, a well-implemented quick sort can generally be run at (O (NLOGN) time.

Here are some common notation:

Accessing an element in an array is a constant-time operation, or an O (1) operation. An algorithm that removes half of the data elements in each step, such as a binary search, usually takes O (LOGN) time. Using strcmp to compare two strings with n characters requires O (n) time. The regular matrix multiplication algorithm is O (n^3), as it is calculated that each element needs to multiply n on the elements and add them together, and the number of all elements is n^2.

The exponential time algorithm usually comes from the need to require all possible results. For example, the collection of n elements has a 2n subset, so the algorithm that requires all subsets will be O (2n). The exponential algorithm is generally too complex, unless the value of n is very small, because adding an element to the problem results in a doubling of elapsed time. Unfortunately, there are many problems (such as the famous "tour salesman Problem"), and the algorithms that have been found so far are exponential. If we do encounter this situation, we should usually replace it with an algorithm that looks for the best results.

Summary of Time complexity:

Inter-level complexity is an equation (unit) used to estimate the run time of the algorithm.

In general, algorithms with high time complexity are faster than algorithms with low complexity.

Common time complexity (sorted by efficiency)

O (1) <o (log2n) <o (n) <o (nlog2n) <o (n2) <o (n2log2n) <o (n3)

Uncommon time complexity (look just fine)

O (n!) O (2n) o (NN) ...

How to judge the complexity of time at a glance?

The process of halving the cycle? O (LOGN)

Several loops are the complexity of N's several sides.

3. Complexity of space

Spatial complexity (space complexity) is a measure of how much storage space is temporarily occupied by an algorithm while it is running, and is recorded as S (n) =o (f (n)). For example, the time complexity of direct insertion sequencing is O (n^2), and the spatial complexity is O (1). The general recursive algorithm will have O (n) space complexity, because each recursive to store the return information. The advantages and disadvantages of an algorithm are mainly measured from the execution time of the algorithm and the storage space required to occupy two.

The spatial complexity of an algorithm s (n) is defined as the storage space consumed by the algorithm, and it is also a function of the problem size n. Asymptotic spatial complexity is also often referred to as spatial complexity. Spatial complexity (spacecomplexity) is a measure of the amount of storage space that is temporarily occupied by an algorithm while it is running. The storage space occupied by an algorithm in the computer memory, including the storage space occupied by the storage algorithm itself, the storage space occupied by the input and output data of the algorithm and the storage space occupied by the algorithm in the running process three aspects. The storage space occupied by the input and output data of the algorithm is determined by the problem to be solved, which is passed by the calling function by the parameter table, and it does not change with the algorithm. Storage algorithm itself occupies the storage space and the length of the algorithm written in proportion, to compress the storage space, you must write a shorter algorithm. Algorithm in the operation of the temporary occupied storage space varies with the algorithm, some algorithms only need to occupy a small amount of temporary work units, and does not vary with the size of the problem, we call this algorithm is "in place \", is to save the memory of the algorithm, Some algorithms need to occupy the number of temporary working units and solve the problem of the size of N, it increases with the increase of N, when n is large, will occupy more storage units, such as fast sorting and merging sorting algorithm is the case.

Analyzing the storage space occupied by an algorithm should be considered synthetically. For recursive algorithm, generally is relatively short, the algorithm itself occupies less storage space, but the runtime needs an additional stack, thus occupying more temporary work units, if written as a non-recursive algorithm, generally may be longer, the algorithm itself occupies more storage space, but the runtime will probably need less storage units.

The spatial complexity of an algorithm only considers the size of the storage space allocated for the local variables during the run, including the storage space allocated for the parametric in the parameter table and the storage space allocated for the local variables defined in the function body two parts. If an algorithm is a recursive algorithm, its spatial complexity is the size of the stack space used by recursion, which is equal to the size of the temporary storage space allocated for a call multiplied by the number of times called (that is, the number of recursive calls plus 1, and this 1 represents a non-recursive call to start). The spatial complexity of the algorithm is usually given in order of magnitude. If the spatial complexity of an algorithm is a constant, that is, it does not change with the size of the processed data n, it can be represented as O (1); When the spatial complexity of an algorithm is proportional to the logarithm of the base N of 2, it can be represented as O (log2n), when the spatial complexity of an algorithm is linearly proportional to N, can be represented as O (n). If the parameter is an array, it is only necessary to allocate a space for it to store an address pointer transmitted by the argument, that is, a machine word space, and if the formal parameter is a reference, it is only necessary to allocate a space for it to store the address of the corresponding argument variable. To automatically reference the argument variable by the system.

Time complexity vs. Spatial complexity:

For an algorithm, its time complexity and spatial complexity are often influenced by each other. When the pursuit of a better time complexity, the performance of the spatial complexity may be poor, that is, may lead to more storage space; Conversely, when the pursuit of a better spatial complexity, the performance of time complexity may be poor, which may lead to a longer run time. In addition, all the performance of the algorithm has more or less mutual influence. Therefore, when designing an algorithm (especially large algorithm), we should consider the performance of the algorithm, the frequency of use of the algorithm, the size of the data amount processed by the algorithm, the characteristics of the algorithm description language, the machine system environment of the algorithm running, and so on, to design a better algorithm. The time complexity and spatial complexity of the algorithm are called the complexity of the algorithm.

Second, find

List lookup: Find the specified element from the list

Input: list, find element

Output: element subscript or no element found

There are two common ways to find:

2.1 Sequential Lookups

Start with the first element of the list and search in order until you find it.

The average lookup length when finding success is: (assuming the probability of each data element is equal) ASL = 1/n (1+2+3+...+n) = (n+1)/2;

When the lookup is unsuccessful, a n+1 comparison is required, with a time complexity of O (n);

Therefore, the time complexity of sequential lookups is O (n).

Implementation of sequential lookups:

2.2 Two-part search

Starting from the candidate area of the ordered list Data[0:n], the candidate area can be halved by comparing the value of the lookup with the median of the candidate area.

Description: The element must be ordered, and if it is unordered, it should be sorted first.

The basic idea: Also known as binary lookup, belongs to the ordered search algorithm. With the given value K first compared with the middle node of the keyword, the middle node divides the Line table into two sub-tables, if the equality is found successful; if not equal, then according to the comparison between K and the middle node keyword to determine which child table next, so recursively, until the find or find the end of the discovery table does not have such a node.

Complexity analysis: In the worst case, the number of keyword comparisons is log2 (n+1), and the expected time complexity is O (log2n);

Note: The precondition of binary lookup is that ordered table sequential storage is required, for static lookup table, once sorting no longer changes, binary lookup can get good efficiency. However, for datasets that require frequent insert or delete operations, maintaining an ordered ordering can have a small amount of work, which is not recommended.

Python Learning-09 (Find, sort, and talk about data structures)

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.