An algorithm of complexity
The same problem can be solved by different algorithms, and the quality of an algorithm will affect the efficiency of the algorithm and even the program. The purpose of the algorithm analysis is to select the suitable algorithm and the improved algorithm. The evaluation of an algorithm is mainly considered in terms of time complexity and spatial complexity.
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.
Two recursion
def foo (x):ifx = =1: Print ('Foo') Else: foo (x-1) print (x) def bar (x):ifx = =1: Print ('Bar') Else: Print (x) bar (x-1) foo (4) Print ('='* -) Bar (4)
Feel the result of the output
Foo 2 3 4====================432Bar
It is not difficult to understand that the recursion is Foo or bar, but the normal front of the execution of the implementation of the AH.
With a box model, it's clear.
So, if you want to input xxxxxxxxxhello,worldooooooooo, like this effect, with recursion, you can write this.
def foo (n):ifn = =0: Print ('Hello,world', end="') Else: Print ('xx', end="') foo (n-1) Print ('oo', end="') foo (4)
Output:
Xxxxxxxxhello,worldoooooooo
Basic concepts of algorithms