Complexity of Time
The time complexity of the algorithm is a function, it quantitatively describes the algorithm's running time, time complexity commonly used "O" expression, when using this method, the time complexity can be called asymptotic, it examines when the input value size approaching infinity
Time complexity is a formula used to estimate the run time of an algorithm (unit), in general, the algorithm with high time complexity is slower than the algorithm with low complexity
Print (' Hello World ') # O (1) # O (1) print ("Hello World") print (' Hello Python ') print (' Hello algorithm ') for I in range (n) : # O (n) print (' Hello World ') for I in Range (n): # O (n^2) for J in range (n):p rint ("Hello World") for I in Range (n):
# o (n^2) print (' Hello World ') to J in range (n):p rint ("Hello World") for I in Range (n): # O (n^2) to J in range (i):p rint (' Hello World ') for I in Range (n): for j in Range (N): A for k in range (n):p rint (' Hello World ') # O (n^3)
Several loops are the time complexity of N's several sides
n = 64while n > 1:print (n) n = n//2
64,log264 = 6, so the time to halve the cycle is O (log2n), i.e. O (logn)
If it is the process of halving the cycle, the time complexity is O (logn) or O (log2n)
Common time Complexity ranking: O (1) <o (LOGN) <o (n) <o (Nlogn) <o (n2) <o (N2logn) <o (n3)
Complexity of space
Spatial complexity: An equation used to evaluate the size of an algorithm's memory footprint
A = ' python ' # spatial complexity is 1a = ' python ' b = ' PHP ' c = ' Java ' num = [1, 2, 3, 4, 5] # spatial complexity of 5num = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]] # space complexity is 5*4num = [[[1, 2], [1, 2]], [[1, 2], [1, 2]], [[1, 2] , [1, 2]] # space complexity of 3*2*2
Define one or more variables, spatial complexity is 1, and the spatial complexity of the list is the length of the list
Python (algorithm)-time complexity and space complexity