01 algorithm data structure / algorithm Architectural Overview

01 algorithm data structure / algorithm Architectural Overview

1. Algorithm

  • Algorithm Overview

    It is the essence of computer algorithms to process information, because a computer program is essentially an algorithm that tells the computer the exact steps to perform a specified task. Generally, when the information processing algorithm, reads data from the storage device or the address input data, writes the result to an output device or a memory address for later recall.

    Algorithm is independent of the presence of a problem-solving methods and ideas.

    For the algorithm, language is not important, important is the idea.

  • Five characteristics of the algorithm

    1. Input : 0 or algorithm having a plurality of inputs
    2. Output : algorithm outputs at least one or more of
    3. Finite resistance : after a limited step algorithm automatically closed without an endless loop, and each step can be completed within an acceptable time
    4. Uncertainty : Algorithms Each step has a definite meaning, does not appear ambiguous
    5. Feasibility : Each step of the algorithm are feasible, which means that every step possible to complete a limited number of executions

2. The method of judging the merits of the program

  • Consume computer resources and efficiency (not intuitive)

  • Consuming calculation algorithm execution (not recommended, as it will be affected machines and execution environment)

  • Time complexity (recommended)

3. Time Complexity

  • Number / quantization step algorithm executed: Rule Evaluation

  • Expressions: Big O notation

    • The most important item: time complexity of expression of the most significant items
    • O (most important item): O (n), O (n ** 2)
  • Common time complexity:

    O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(n^3) < O(2^n)  < O(n!) < O(n^n)

    Code Example: calculating the time complexity of the following algorithm

    a=5
    b=6
    c=10
    for i in range(n):
       for j in range(n):
          x = i * i
          y = j * j
          z = i * j
    for k in range(n):
       w = a*k + 45
       v = b*b
    d = 33
    
    # 时间复杂度:4+3n**2+2n==>n**2

4. Data structure

  • Concept: For data (basic data types (int, float, char)) of the organization was known as a data structure. How is a set of data structures solved to save, save what form.

Performance Analysis of the data structure 5. python

  • timeit Module: This module may be used to test the speed of execution of code python section length / time.

  • Timer class: class is length / time timeit python module designed to measure code execution speed. Prototype: class timeit.Timer (stmt = 'pass', setup = 'pass').

    • stmt argument: that the upcoming test code block statements.
    • setup: setting necessary for the operation's code block.
    • timeit function: timeit.Timer.timeit (number = 100000), the function returns the average time statement execution block number of times.
  • Code Example:

    import timeit
    def test01():
        alist = []
        for i in range(1,1001):
            alist += [i]
        return alist
    def test02():
        alist = []
        for i in range(1,1001):
            alist.append(i)
        return alist
    def test03():
        alist = list(range(1,1001))
        return alist
    def test04():
        alist = [i for i in range(1,1001)]
        return alist
    if __name__ == '__main__':
        #stmt:待执行的代码块
        #setup:
        timer = timeit.Timer(stmt='test01()',setup='from __main__ import test01')
        print(timer.timeit(5000))
    
        timer1 = timeit.Timer(stmt='test02()',setup='from __main__ import test02')
        print(timer1.timeit(5000))
    
        timer2 = timeit.Timer(stmt='test03()',setup='from __main__ import test03')
        print(timer2.timeit(5000))
    
        timer3 = timeit.Timer(stmt='test04()',setup='from __main__ import test04')
        print(timer3.timeit(5000))
    
    """ 
    0.41899349999999913
    0.4362713999999954
    0.0754374999999925
    0.2028786999999994
    """  

6. Summary

  • Use a different form of organization data, based on the complexity of the time when a query is not the same. Therefore considered algorithm is designed to solve practical problems, the data structure is a vector algorithm to deal with the problem.

Guess you like

Origin www.cnblogs.com/liubing8/p/12059415.html