Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Python algorithm

Shulou Source: shulou.com Published: 2022-06-03 00:20:01 11月17日 Update

Complexity of the algorithm

The time complexity of the algorithm refers to the time resources consumed by the algorithm.

Time complexity is expressed by "O (order of magnitude)".

Common time complexities are:

O (1) constant order; the larger the scale of the problem is, the higher the efficiency is, and the time is the same, a = [1pr 2jue 3] a [0] = 1je a has no effect.

O (log2n) logarithmic order: the larger the scale of the problem, the higher the efficiency, and the slower the time increases.

O (n): linear order, time increases linearly with data size, time increases normally, example: for i in range (n)

O (N2): square order, time increases exponentially with the increase of data size, time increases rapidly, for i in range (n): for j in range (I):.

N represents the problem scale.

The time spent in the algorithm is proportional to the number of sentences executed in the algorithm.

Space complexity S (n)

The space complexity of a program refers to the amount of memory needed to run a program.

Data exchanging

Sort by three numbers

Temporary variable t of axi11, bread9, canti8

[root@133] # vim change.py #! / usr/bin/pythondef swap (if a > b: t = an a = b b = t if a > c: t = an a = c c = t if b > c: t = b b = c = t print

If name = 'main':

Swap (11, 9, 8)

Example 2:list An is all 0, modify part of the list element to 1, enter 5 elements manually, and print out the element position where the element is 0.

[root@133 ~] # vim key.py #! / usr/bin/python#encoding:utf8def key (): a = [] for i in range (10): a.append (0) # a = [0pc0pc0p0p0p0p0p0p0p0p0p0pl 0p0pl 0p0pl 0] for i in range (5): input = int (raw_input ("Please input a num:")) # manually enter the list element a [input] = 1 # a [4] = 1 for i in range (len (a)): if a [I] = 0: print iif _ name__ = ='_ main__': key () ~ [root@133] # python key.pyPlease input a num:1Please input a num:2Please input a num:3Please input a num:4Please input a num:506789

Tags: Time complexity complexity algorithm element scale data problem example manual efficiency program space linear input number representative location memory variable Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia OPPO Reno MySQL Linux