The data structure
The way data is organized is called a data structure (int, float, char) lists and dictionaries are also data structures
algorithm
- Algorithms are the essence of how computers process information.
- Algorithm is an independent method and idea to solve problems
Five features of the algorithm
-
The input
-
The output
-
Fineness: The algorithm terminates automatically after a finite number of steps rather than an infinite loop, and each step can be completed in an acceptable amount of time.
-
Deterministic: Each step in the algorithm has a definite meaning, without ambiguity
-
Feasibility: Each step of the algorithm is feasible, that is, each step can be performed a limited number of times
The list
If a+b+c=1000 and a^2+b^2=c^2, how can we find the possible combinations of ABC? Solution 1: Enumeration method
For I in range(1,1001): for j in range(1,1001): k = 1000 -i-j if I * I + j*j == k*k: print(I,j,k)Copy the code
Algorithm complexity and large O notation
Factors affecting the efficiency of operation
- The execution efficiency of the machine
- Complexity of the algorithm
- But in general, the total time of execution varies from machine to machine, but the amount of basic operations performed is roughly the same
Time complexity
-
Definition:
In computer science, time complexity, also known as time complexity, is a function that qualitatively describes the running time of an algorithm. This is a function that represents the length of the string input to the algorithm. The time complexity is often expressed in large O notation, excluding the lower order and leading coefficients of the function. In this way, the time complexity can be said to be asymptotic, that is, as the size of the input approaches infinity.
-
Optimal time complexity, worst time complexity, average time complexity
- Consider worst-case complexity: guaranteed performance
- Consider the average time complexity
Time complexity comparison
The O (1) < O (logn) < O (n) < O (nlogn) < O (n ^ 2) < O (n ^ 3) < O (2 ^ n) < O (n!) < O(n^n)
The pros and cons of algorithms in Python
Timeit modules
from timeit import Timer
Copy the code
Speed: 4 > 3 > 1 > 2