Algorithms data structures

Algorithms + data structures = programs

Data structure is a description of the data (operation object), and the type and organization, the algorithm is data describing the operational steps

The concept of algorithm

characteristic:

1. finite exemplary:
algorithm in each step can be completed within a limited time (programDo not satisfy this condition)
2. Uncertainty:
every step of the algorithm must be precisely defined, only one algorithm execution path under any conditions
3. Feasibility:
all actions necessary to implement the algorithm in the basic operation of a limited number of operations have been implemented
4. input:
an algorithm should be zero or more input
5. output has:
an algorithm should have one or more outputs

Evaluation criteria algorithm

  1. Correctness:
    Program No syntax error; for (groups or harsh or all of) the input data can be drawn from the results meet the requirements of the specification
  2. Readability:
    the ease of understanding of the algorithm is
  3. Robustness:
    resistance to illegal input
  4. High efficiency and low memory requirements:
    efficiency (algorithm execution time), storage (memory requirement during the maximum storage space) are related to the scale of the problem

Description of the algorithm

Description Tool

Natural language, block diagram, high-level programming language

Performance Analysis

Methods to measure the efficiency of the algorithm

method the difference
Prior analysis estimation method Compare advance, moreCommon
Later statistics Necessary to run the program in the computer field, there are other factors that easy to change the nature of the algorithm

Algorithm execution time-related factors

  1. Algorithmic strategies adopted;
  2. Scale algorithm to solve the problem;
  3. Programming language used;
  4. Quality of the machine code generated by the compiler;
  5. Execution speed of the computer algorithms;
    after three subject to computer hardware and software

A running time of only depend on the particular algorithmScale of the problem

First, the time complexity

  • Definition:
    the required assessment of the implementation programtimeCan be estimated procedureprocessorThe extent of use;

  • Statement frequency (frequency of the time)
    the number of the statement in the repeatedly executed in an algorithm, referred to as T (n);

  • Time complexity
    in general, the number of the basic algorithm operation is repeated n is a function of the problem size, with T (n) represents, if an auxiliary function F (n), such that when n approaches infinity , T (n) / f (n) is not equal to zero limit constant, called f (n) is T (n) of the same order function, referred to asT(n)=O(f(n))It is called asymptotic time complexity of the algorithm, for short time complexity.

  • For example

  • Constant order
    statement is executed, the time complexity is O (1);
    int sum = 0,n=100; sum = (n+1)*n/2; printf("%d",sum);

  • Linear order
    mainly to analyze the operation of the loop structure

for( int i = 0; i < n; i++){
//时间复杂度为O(1)的算法
......
}此算法时间复杂度为O(n)
  • To order
int num = 1;
while( num < n){
num*=2;
//时间复杂度为O(1)的算法
......
}
//假设循环的次数为X,则由2^x=n得出x=log₂n,
//因此得出这个算法的时间复杂度为O(log₂n)

Order of the square

for( int i = 0; i < n; i++){
   for(int j = 0; j < n; j++){
     //时间复杂度为O(1)的算法;
     ............
     }
  }
  //循环次数为 n² ,时间复杂度为O(n²)
  • Comparison of
    temporal contrast complexity
    Here Insert Picture Description
    Here Insert Picture Description
    common time complexity in a time consuming small to large is
O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(n^3) < O(2^n) < O(n!)

Second, the space complexity

  1. Definition:
    the required assessment of the implementation programstorageCan be estimated procedureRAMThe extent of use,
    denotedS(n) = O(f(n))
  2. Space complexity required for consideration
    input data, the program itself, auxiliary variables
Published 26 original articles · won praise 6 · views 1453

Guess you like

Origin blog.csdn.net/qiao_xi/article/details/101003677