Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python algorithm

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report