Large data structures and algorithms O module notation timeit

Common data structure: Stack Queue list binary deque

Algorithms: Binary Search Select Quick Sort Bubble sort binary tree insertion Hill

What is computer science?

  • First clear that not only is the computer science research on the computer, although the computer has played a major role in the development of science, but it is only a tool, not a tool for the soul of it. Research solutions produced by the process of so-called computer science is actually solve problems and solve problems in production. For example, given a problem, computer scientists goal is to develop an algorithm to deal with the problem, the solution to this problem finally obtained, or the optimal solution. So computer science can also be considered is the study of algorithms. Therefore, we can also feel, is the so-called algorithms to deal with the problem and solving a realization of ideas or ideology.
 

Understand how the algorithm visualization

  • A blow will be to develop strategies before the war, the purpose is to be able to cut costs in the shortest time at the lowest consumption conditions to obtain the final victory. If an encoder as a battlefield, the programmer is the commander of the battle, you can how your program can and with minimal consumption of resources to obtain the final results of the implementation of it in the shortest? Algorithm is our strategy!

significance

  • Universal data structures and algorithms thinking abnormal strong, have been used in any language, they will be our coding career with us for the longest weapon (right-hand man). There are some experienced programmers final fight is the algorithms and data structures.
  • Data structures and algorithms thinking can also help us to expand thinking and coding experience, it allows us to better integrate into every corner of the world fall programming.
 

What is the algorithm analysis?

  • Students will often new to programming procedures and others have written procedures to do than to get in the process of alignment of the two sides will find a program written in a very similar but not the same. Then there will be an interesting phenomenon: two sets of procedures are used to solve the same problem, but the two programs looked so different, so which group the program better?

For example:

  • = B + C + A 1000 A 2 + B 2 ** 2 = C (a, b, c are natural numbers), obtaining a, b, c the possible combinations of?
#方案一
for a in range(0,1001):
    for b in range(0,1001):
        for c in range(0,1001):
            if a+b+c == 1000 and a**2 + b**2 == c**2:
                print(a,b,c)

#方案二
for a in range(0,1001):
    for b in range(0,1001):
        c = 1000 - a - b
        if a+b+c == 1000 and a**2 + b**2 == c**2:
            print(a,b,c)

Judge the merits of the program method

  • Consume computer resources and efficiency (not intuitive)
  • Consuming calculation algorithm executed
  • Time complexity (recommended)
Time complexity 
Judging Criteria: quantization operation performed by the algorithm / number of the steps of 
the most important items: the time complexity of expression in the most meaningful items 
using Big O notation to represent the time complexity: O (quantitative expression algorithm to step the most significant one formula)
  • Case: calculate 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

###########################
3 + 3*n*n + 2*n + 1
3n**2+ 2n + 4 
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)

 

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.
 
  • Case: Some students need to store student information (name, score), then these data should be how to organize it? What is the time complexity of the query on a specific student is it? (Three tissues mode)

 

  • Three forms of organization based on the time complexity of the query?
  • 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.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  • Goal of this section is to tell you the big O performance of Python lists and dictionaries operations. Then we will do some experiments on the benefits of spending time to explain and use the data structure of each data structure
The practical operation of 
the list of operations have a very common programming task is to increase a list. We immediately think of two ways to create a longer list, you can use the append method or the concatenation operator. But higher efficiency that these two methods do. This is very important for you, because it can help you to improve the efficiency of your own program by selecting the appropriate tool. 
Examples of an empty list, then a 0 - add data to the list of n range. (Four ways) 
DEF Test01 (): 
    alist = []
     for I in Range ( 1000 ): 
        alist.append (I) 
DEF Test02 (): 
    alist = []
     for I in Range ( 1000 ): 
        alist + = [I ] 
DEF TEST03 (): 
    alist = [I for I in Range ( 1000)]
def test04():
    alist = list(range(1000))
  • 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.

from timeit import Timer
if __name__ == '__main__':
    t = Timer('test01()','from __main__ import test01')
    s = t.timeit(1000)
    print(s)

  • Calculate the average run time-consuming

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/XLHIT/p/11360283.html