python-data structure-DAY1

0. The basic characteristics of the algorithm
0. There are 0 or more inputs.
1. There are 1 or more outputs
2. The steps executed by the algorithm are infinite
3. The meaning of the sentences executed at each step is definite
4. The design idea of ​​the algorithm must be feasible

1. The algorithm is measured by the time complexity "Big O"
because the execution time of each machine is always different, but the number of execution costs is roughly the same. In order to measure this roughly the same concept, we introduce time complexity, Time complexity includes optimal time complexity, worst time complexity, and average time complexity. In general, the worst case complexity is studied first (the order of data is disordered).
Sequential structure---complexity addition
Loop structure---complexity multiplication
Branch structure---complexity in the worst case in the branch,
ignore the constant term (O(1))

2. Common time complexity and size comparison
constant term--------O(1)
linear term--------O(N)
square term---------O( N²)
cubic term---------O(N³)
logarithmic term---------O(logN)
exponential term---------O(a^N)
NlogN ----------O(NlogN)

Insert picture description here

3.timeit measures the time complexity of python code
timeit measures time

from timeit import Timer


def t1():
    l1 = []
    for i in range(1000):
        l1.append(i)


text_time1 = Timer("t1", "from __main__ import t1")
print("append_time:{}".format(text_time1.timeit(1000)))



append_time:1.3199999999990997e-05

Common operation complexity of list

4.timeit measures the complexity of the list
dict operation time complexity

Guess you like

Origin blog.csdn.net/soulproficiency/article/details/104505844