Embedded team training _ data structures and algorithms Overview

Requirements: to understand and remember, it will solve the time complexity of the algorithm

Summary: Programming = data structure + algorithm
explanation: data structures used to solve data storage problems, and algorithms for processing and analyzing data. (Catalog, page forward and backward)

A: Data Structure

Data: All the symbols can be entered in the computer. We usually includes not only the use of integer, floating point, etc., as well as character, sound, pictures, video (to become character data by coding) and so on.

Straightforward to understand, it is the study of logic and data storage a discipline. Simply divided into: a data logical structure (logical relationship) and the
physical structure
. It is based on some form together a collection of data organization, which not only stores data, but also support the operation of accessing and processing data ( CRUD ).

Large about exam content: Remember that you can

1. The logical structure:

The logical structure data, simple understood refers to the logical relationships between data.

Between the data logic can simply be divided into three categories:

  • One (linear structure, string)
  • To-many (hierarchical tree)
  • To-many (Figure)

2. Physical structure

It refers to data stored in a computer memory mode (e.g.: memory) can be selected stored centrally or distributed storage.

Specific performance: sequential storage (continuous address) and chain storage (pointer indicates the logical relationship).

3. Abstract Data Types

Abstract data types ADT: a mathematical model and a set of operations defined in the model. (To put it plainly, the specification is a)
a series of data structures are explained in the following abstract data types.

II: Algorithms

Algorithm for solving a problem need to follow a set of simple instructions are clearly specified. In the computer, the algorithm refers to the accurate and complete description of the solution.

When problem solving, algorithm first have to solve the problem of the heart, around the algorithm to write code.

Therefore, programming data structures = Algorithms +

Large about exam content: Remember that you can

1. The five basic features of the algorithm:

  1. Input
    input parameters, the number (> = 0), for example: direct print, it is no parameter.
  2. Output
    output and a return value, the number (> 0), must have an output , at least to have a print.
  3. Finite nature
    limited the steps after not an infinite loop, completed within a limited time.
  4. Uncertainty
    every step must have a specific meaning, it does not appear ambiguous.
  5. Feasibility
    have talked about to be completed within a limited time, that every step is feasible , each step is for a limited time. (So the overall finish in finite time)

2. The algorithm design requirements

  1. Correctness (a little): get the right answer question (nonsense)
  2. Readability: Easy to read between programmers, understanding, communication, help people understand the algorithm, easy to modify debugging. For example: Comment
  3. Robustness: the input data (parameters) for legitimacy check . When the input data is not legitimate, the algorithm will be related to treatment, rather than the exception.
  4. Try to meet high time efficiency and low memory requirements ( time and space complexity of low complexity )

3. The time and space complexity

(1) :( statistical methods ex post slightly)
by designing a good test program (test) data and then compare the running time, which was to determine the efficiency.

(2) estimation method to estimate in advance
before the program, according to the statistical method of estimation algorithm.

(3) time and space complexity

The time complexity of the algorithm, the algorithm used mainly depends on the number of cycles to code loop structure (referred to as "frequency"). The fewer the number, the lower the time complexity of the algorithm.

Resetting scale data input is n

a) ++x; s=0;

b) for (int i = 1; i <= n ; i++) { ++x; s += x; }

c) for (int i = 1; i <= n; i++) { 
for (int j = 1; i <= n; j++) { ++x; s += x; } 
}

The frequency f (n) are 1, n, n ^ 2.

The time complexity of the algorithm, the algorithm measures the time, remember: T (n) = O (f (n))

a time complexity of O (1), b the time complexity is O (n), c is the time complexity is O (n ^ 2).

If a, b, c three examples of the composition of a program, the time complexity of the algorithm is O (n ^ 2 + n + 1).
However, such a representation is wrong, but also for n ^ 2 + n + 1 for simplification.

Simplified procedure summarized in three steps:

1. Remove all constant run time of the addition. (E.g. n ^ 2 + n + 1, 2 + ^ direct becomes n-n-)
2. only the most items. (E.g. n ^ 2 + n ^ 2 becomes n-)
3. If the highest entry exists but is not a factor, the coefficient removed. (E.g., the coefficient is 1 n ^ 2)

Therefore, eventually a, b and c merger time code complexity is O (n ^ 2).

常数阶:O(1)(单纯的分支结构也是执行一次)

int sum = 0,n = 100;  /* 执行一次 */
sum = (1 + n) * n/2;  /* 执行一次 */
printf("%d", sum);    /* 执行一次 */
if(sum == 0) {        /* 执行一次 */
    printf("%d", sum);
} else {
    printf("-1");
}
线性阶:O(n)(循环结构)

int i;
for (i = 0; i < n; i++) {  /* 执行n次 */
     /* ......(里面是O(1)) */
}
对数阶:O(logn)(迭代)

int count = 1;
while (count < n) {
    //2^x=n   x=logn
    count = count * 2;
     /* ......(里面是O(1)) */
}
平方(次方)阶:O(n^2),O(n^3)....

int i,j;
for(i = 0; i < n; i++) {
    
    for(j = 0; j < n; j++) {
     /* ......(里面是O(1)) */
    }

}
循环嵌套
思考:下列代码的时间复杂度是?
(1int i,j;
for(i = 0; i < n; i++) {
    
    for(j = i; j < n; j++) {  /* 注意这里j=i */
     /* ......(里面是O(1)) */
    }

}2int n;
void function (int count) {
	int j;
	for(j = count; j< n; j++) {
		/* ......(里面是O(1)) */
	}
}
---------------------------------
n++;
int i,j;
function(n);
for(i = 0; i < n; i++) {
    
    for(j = i; j < n; j++) {  /* 注意这里j=i */
     /* ......(里面是O(1)) */
    }

}

Third, work

1, the time complexity of the block is below ()

s = 0;
for(i=0; i<n; i++)
		for(j=0; j<n; j++)
s += B[i][j];
sum = s;

2, the time complexity is the following program segment ()

for(i=0; i<n; i++)
	for(j=0; j<m; j++)
		A[i][j] = 0;

3, the time complexity is the following program segment ()

i=1;
while(i<=n)
	i = i*3;
Published 47 original articles · won praise 18 · views 4855

Guess you like

Origin blog.csdn.net/qq_43605085/article/details/103015404