Python (algorithm)-time complexity and space complexity

Source: Internet
Author: User

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

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.