Basic concepts of algorithms

Source: Internet
Author: User

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

Related Article

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.