Introduction two temporal data structure

By the previous chapter, https://www.cnblogs.com/X404/p/11984707.html can know the data structure is divided into

1. The logical structure comprising:

1) collection

2) linear structure

3) tree

4) the structure of FIG.

2. storage structure

1) sequential storage

2) Chain store

3) index storage

4) hash storage

More emphasis is needed to know,

Ado, to go along with the first chapter, the algorithm should be designed to satisfy my:

1. validity: to ensure correct

2. Readability: Is easy to understand

3. Robustness: when you enter invalid data, whether an error

4. Time and Space: Analysis of time complexity and space complexity of the algorithm to improve efficiency (Key)

 

Choose the best algorithm for each measure 2:

Time Complexity: The total number of steps required to run the algorithm (run from start to break point)

Space complexity: (size of the storage space after running) when the algorithm is executed share storage space

 

Reasonable choice of one or several operating as a "normal operation"

Each algorithm is executed to determine how many times the standard operation, and the algorithm to calculate the amount specified number of times this position

 

Determining the amount of computation time complexity:

Worst case time complexity of the algorithm: Algorithm to calculate the maximum amount when all inputs to calculate the amount of

(Worst case time complexity is equivalent to the baton Games, the total time from start to finish with the worst case is referred to the complexity of the time)

Average case time complexity of the algorithm: calculated weighted mean value algorithm in a calculation amount for all the input

(Average time complexity of the case, the relay may still be used for games, over time, the average value is called the weighted average)

The worst case time complexity and average case complexity collectively referred to as time complexity  

+ Worst case time complexity of the weighted average complexity of "time complexity = (worst and are included in the weighted time complexity)

void max(int a,b,c,d)
{a*=d;b*=d,c*=d;
if (a>b)x=a;
else x=b;
if (c>x)x=c;
printf ( " % d \ the n- " , the X-);}
 / * have time to pen to count on the worst time complexity is how much? * /

 

Common time complexity by an order of magnitude ascending once for:

Constant O (1): no matter how many lines complex structure cycle or the like are executed O (1)

int i,j;
i=2;
j=3 i
++; j++
i=i+j;

 

On the order of O (long2n):

 1 int a; 2 while(a<n){ 3 a=a*2; 4 } 

Inside the while loop, each time will a multiplied by 2, after completing ride, a distance n is getting closer. We try to solve it, then assume x cycle times, a will greater than 2, and this time the loop exits, and that is equal to the power of 2 x n, then x = log2n
That is when the cycle log2n code End. Thus the time complexity of this code is: O (logn)

 

Linear order O (n):

1 int i;
2 for(i=0,i<n,i++){
3 printf("*******");}

This code, which code for loop is executed n times, so time consuming that it is changes with changes in n, so this type of code can be O (n) is represented by the time complexity.

 

On the order of linear (nlong2n) O:

Linear logarithmic order O (nlogN) actually very easy to understand, the time complexity is O (logn) code loop N times, then its time complexity is n * O (logN), it wants to O (nlogN) .

1 for(i=1,i<n,i++){
2 j=1;
3 while(j<n){
4 j=j*2;}}

 

Squared order O (n2),

Polynomial order OnC),

Index order O (Cn)

We can be the time complexity of several data input function of the size n, if n2 solve the problem you need to perform operations, is recorded as O (n2)

 

The relationship between time complexity and time

 

 

Space complexity:

Is an algorithm during operation temporary occupation measure of the size of the storage space.

An amount of storage space needed during the execution of the algorithm comprises the following parts:

The space occupied by the program code;

The space occupied by the input data;

Auxiliary variable space occupied;

 

Guess you like

Origin www.cnblogs.com/X404/p/12007166.html