I. What the data structure covers
Second, the basic concept of the algorithm:
1, the concept of the algorithm:
Algorithm is a description of the solution step for a particular problem, which is a finite sequence of instructions, where each instruction represents one or more operations.
2, the characteristics of the algorithm:
- Poor sex: Instruction sequence is limited
- Certainty: Each statement has a clear meaning and no ambiguity
- Feasibility: Each statement should be completed within a limited time
- Input: 0 or more inputs
- Output: one or more outputs
3, the difference between the algorithm and the program:
Program:
Program is a sequence of instructions (statements) that a software developer develops in a programming language that is appropriate for a computer to execute according to a user's needs.
Four elements included in the program:
data structure + algorithm + Programming method + Program language
The program differs from the algorithm. A program is a specific implementation of an algorithm using a programming language. The program can not satisfy the poor of the algorithm, such as the operating system is also a program, if you like, your computer can be open, keep the operating system running.
4, the relationship between the program, algorithm, software:
Program: The computer implementation of the algorithm. Implemented in a programming language.
Algorithm: Represents the solution of the problem.
Software: Programs + documentation
Program Design = data structure + algorithm
Third, the data type:
1. Data type:
Refers to a collection of values and a collection of operations that are defined on the collection. For example, the integer type of Java, which is not only a collection of integer values, but also a collection of operations on integer types, plus, minus, multiply, divide, modulo, and so on.
The type of data that we typically refer to is a basic data type that is supported by a high-level language. Such as Java's basic data types: int, double, float, char, and so on.
Data types in Java:
2. Abstract data type:
A mathematical model and a set of actions defined on the model (defined by the user to represent the data model of the application problem).
It seems that the definitions of abstract data types and data types are basically the same. The difference is that the data type usually refers to a high-level language, while the abstract data type is redesigned by the user, a concept. The meaning of "abstraction" here is mathematical abstraction.
- Atomic type: integer type, for example
- Fixed aggregation types: such as complex numbers, two real numbers determine the order of composition
- Variable aggregation types: For example, the car type, the composition of the composition is indeterminate. (Because the composition of the car, the big truck is different)
The higher the level of abstraction for abstract data types, the higher the reusability. For example: Object in Java is an abstraction of all objects, so it can be used as the parent class for all classes.
Five, the design goal of the algorithm:
1, the design objectives of the algorithm:
(1) Correctness: Meet the specific problem of the solution, the basic goal.
(2) Readability: beneficial to people to understand the algorithm.
(3) Robustness: Input illegal data, can be properly processed, does not produce inexplicable output.
(4) Efficiency: including time efficiency ( time complexity ) and space efficiency ( space complexity ).
2. Algorithm Performance index:
(1) The time efficiency of the algorithm is also called the time complexity.
(2) The spatial efficiency of the algorithm is also called spatial complexity.
(3) The same problem can be solved by different algorithms, and the quality of an algorithm will affect the efficiency of the algorithm and even the program. The purpose of the algorithm analysis is to select the suitable algorithm and the improved algorithm. The evaluation of an algorithm is mainly considered in terms of time complexity and spatial complexity .
(4) The efficiency of the algorithm time and space efficiency are usually contradictory . All generally only take a balance point. (Here is a philosophical thought, the study of computers, is not the concept of time and space, fish and bear can not be combined with OH)
(5) Usually we assume that the program is running in memory, and that the memory capacity is sufficient to use, so it is more about time complexity.
Vi. time complexity of the algorithm:
The time complexity of the algorithm reflects the length of time the algorithm executes. There are usually two ways to measure the time an algorithm executes on a computer:
1. Post-mortem statistical method:
2. Pre-analysis method: (Common)
Write the high-level language used by the algorithm
Machine language code quality generated by programming
Machine instruction Execution Speed
Problem size
Note: The time complexity of the algorithm is determined by the frequency of the deepest nested statements.
2, O () function:
The most common function that represents the time efficiency of an algorithm and the number of data elements processed by the algorithm n function, the O () function.
Defined:
Under normal circumstances, the number of iterations of the basic operation of the algorithm is a function of the problem size n, denoted by T (n), if there is an auxiliary function f (n), so that when n approaches infinity, the limit value of T (n)/f (n) is not equal to zero constant, then f (n) is the same order of magnitude function of t As T (n) =o (f (n)), called O (f (n)) is the progressive time complexity of the algorithm, which is referred to as the complexity of time. See the know:
In general, the number of iterations of the algorithm's basic operation is a function of the problem size n, and the time measurement of the algorithm is recorded as
Order ofmagnitude of T (N) =o (f (n)) f (n)----t (n)
Called the algorithm's progressive time complexity.
Frequency t (N): refers to the number of times a statement is executed.
f (N)-- a function on the size of the problem that represents the order of magnitude of T (N), only related to the size of the problem problem (which may be represented by multiple variables),
Generally expressed as an exponent or logarithm of the problem scale, there is no constant term.
For example: 1, NX, Xn, logxn and so on.
situation One: T (N) has nothing to do with problem size n
When the time complexity T (n) of the algorithm is independent of the problem scale N, the time complexity of the algorithm is T (n) =o (1).
Case two: T (n) is related to problem size n
Example 1: Set the array A and B in the previous section has been assigned, to find the following two n-order matrix multiplication algorithm time complexity:
for (I=0;i<n; i++) {for (J=0;j<n;j++) { c[i][j] =0; //Basic statement 1 for (k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k ][j];//Basic Statement 2 }}}
Code
Example 2: set N as the number of data elements processed by the following algorithm, to find the complexity of the algorithm time. The code is as follows:
for (I=1;i<=n; I=i*2){ System.out.println (i);}
Analysis:
Example 3: Analyze the time complexity of the bubbling sorting algorithm. The code is as follows:
//bubble sort algorithm public static void Bubblesort (int[] data) { if (data = = null) { return; } < Data . length-1; i++) { = false; J < data.length-1-I; j + + ) {> data[j + 1]) {int temp = Data[j]; DATA[J] = data[j + 1]; Data[j + 1] = temp; Flag = true; } } if (!flag) { return;}} }
Time Complexity Analysis:
(1) In the best case, the bubble sorting algorithm only needs to traverse once to determine that the array has been sorted, the time complexity is O (n);
(2) In the worst case, a n-1 traversal is required for n (n-1)/2 comparisons and record movement, at which time complexity T (n) =o (n2) of the Bubbling sorting algorithm.
3, time complexity of the size relationship:
The following six algorithms are most commonly used to calculate the time of a polynomial. The relationship is:
The relationship between the exponential time is:
When n takes a very large value, the exponential time algorithm and the polynomial time algorithm have a very wide disparity in the time required.
Small knowledge:
NP(nondeterministic polynomial) difficult problem: refers to the complexity of the algorithm is difficult to use polynomial representation of the problem, the complexity of the algorithm is usually the number of n-level, the conventional algorithm is difficult to solve.
Vii. spatial complexity of the algorithm:
Spatial complexity refers to the order of magnitude of the memory space required by the algorithm during operation. Note: S (n) =o (f (n)), where n is the size (or size) of the problem.
Note: Because the spatial complexity of most algorithms is not serious, and the algorithm's spatial complexity analysis method and algorithm time complexity analysis method is basically the same, so the general data structure only discusses the time complexity of the algorithm, does not discuss the spatial complexity of the algorithm.
code example: Analyze the spatial complexity of the following algorithms
Static void reserse (int[] A,int[] b) { int n= a.length; for (int i=0;i<n;i++) { b[i]=a[n-1-i];} }
In the above code, when the program calls the Reserse (a, B) function, the memory space to be allocated includes: reference A, reference B., local variable n and local variable i;
So f (n) =c, where C is a constant. So the spatial complexity of the algorithm s (n) =o (1).
Eight, Summary:
The time complexity of the algorithm is related to two factors: the maximum number of nested loop layers in the algorithm, and the number of times per loop in the maximum nested loop structure.
In general, algorithms with polynomial time complexity are acceptable, and algorithms with exponential (not logarithmic) time complexity can be used only if n is enough hours. Generally efficient algorithm to be controlled in O (n) or O (log2 N)
Note: This article is reproduced. Original address: http://www.cnblogs.com/smyhvae/p/4724692.html
Java about data structure (reprint)