Data structure Introduction
A data structure is a collection of elements that have one or more specific relationships to each other
Program Design = data structure + algorithm
The data structure is in fact a subject that studies the programming problems of non-numerical computation, as well as the relationship between them and the related problems of operation.
Data is a symbol that describes objective events, is an object that can be manipulated in a computer, is a set of symbols that can be recognized by a computer and can be processed by a computer, which means that the data must have two prerequisites:
- Can be entered into the computer
- Can be processed by a computer program
Data
A data element is a basic unit of data that makes sense, and is usually treated as a whole in a computer, also known as a record
Data item: A data element can consist of several data items
Data item is the smallest unit of data indivisible
Data object: Is a collection of data elements of the same nature, and is a subset of the data
Different data elements are not independent, but there are specific relationships, and we call these relationships structure
Data structure: A collection of elements that exist in one or more specific relationships with each other
Logical structure: Refers to the interrelationship between data elements in a data object.
- Collection structure: Data elements in a collection structure have no other relationship to each other except that they belong to a collection
- Linear structure: The data elements in a linear structure are a one-to-one relationship
- Tree structure: a one-to-many hierarchical relationship between data elements in a tree structure
- Graphical structure: Data elements in a graphical structure are many-to-many relationships
Physical structure: Refers to the storage structure of the logical structure of data in a computer.
- Sequential storage structure: The data element is stored in the address contiguous storage unit, the logical relationship between the data and the physical relationship is consistent
- Chained storage structure: The data element is stored in any storage unit, this group of storage units can be continuous, but also discontinuous.
Data type: Refers to a set of values of the same nature and the general name of some operations defined on this collection.
- Atomic type: is a basic type that cannot be re-decomposed
- struct type: A combination of several types that can be decomposed.
Abstraction refers to the essence of extracting the universal nature of things. (The meaning of extraction is the mathematical abstraction of the data type)
Abstract data type ADT: Refers to a data model and a set of operations defined on the model.
Abstract data types embody the characteristics of problem decomposition, abstraction and information hiding in program design.
Algorithm
Algorithm: A description of the solution steps for a particular problem, represented as a finite sequence of instructions in the computer, and each instruction represents one or more operations.
Features of the algorithm
- Input: with 0 or more inputs
- Output: At least one or more outputs
- Poor: Automatically ends without an infinite loop after a limited number of steps are performed, and each step is completed within an acceptable time
- Deterministic: Each step of the algorithm has a definite meaning and does not appear ambiguity
- Feasibility: Each step of the algorithm must be feasible, that is, each step can be accomplished by performing a limited number of times
Design Requirements for algorithms
- Correctness: At a minimum, there should be no ambiguity in input, output and processing, the need to correctly reflect the problem, and the correct answer to the problem
- Readability: Another goal of algorithmic design is to read, understand and communicate globally
- Robustness: When the input data is not valid, the algorithm can also do the relevant processing, rather than produce abnormal or inexplicable results
- High time efficiency and low storage: Design algorithms should be designed to meet the needs of high time efficiency and low storage capacity
The measure method of algorithm efficiency
- Afterwards statistical method: through the design good test procedure and the data, uses the computer timer to compare the running time of the program programmed by different algorithms, thus determines the efficiency of the algorithm (unscientific, inaccurate)
- Pre-analysis and estimation method: Calculate the algorithm based on statistical method before computer programming
The time the program runs on the computer depends on the following factors:
- The strategy and method adopted by the algorithm
- Code quality generated by compilation
- Input scale of the problem
- Speed of machine execution instructions
In other words, the running time of a program depends on the quality of the algorithm and the input scale of the problem, and the most important thing is to think of the program as an algorithm or a series of steps that is independent of the programming language when analyzing the program's running time.
Time complexity of the algorithm
When judging the efficiency of an algorithm, constants and other minor items in a function can often be ignored, and more attention should be paid to the order of the main item
How to analyze the time complexity of an algorithm? The method is:
Analysis of common time complexity
Simple branching structure (not included in the loop structure) with a time complexity of O (1)
Analysis of the complexity of the algorithm, the key is to analyze the operation of the loop structure
Pure order
int i; for 0; I < n; i++)// time complexity of O (n) {}
Logarithmic order
int 1 ; while (count<n) // since count is multiplied by 2, it is a step closer to N, which means that 2 x is equal to 2 x=log2 n // so the complexity of Time is O (logn) { 2; }
Square Order
int I, J; for 0; I < n; i++)// time complexity O (square of N) { for0; j < N; j + +) { /* The code for this is a sequence of procedural steps with a time complexity of O (1). */ } }
Analyze a relatively complex bit of ...
voidFucintcout//The time complexity of this function is O (n) { for(intj =0; J < N; J + +) { /*The code for this is a sequence of procedural steps with a time complexity of O (1).*/}} N++;//number of executions is 1FUC (n);//number of executions n for(i =0; I < n; i++) {fuc (i);//The entire number of executions is N2 } for(i =0; I < n; i++) { for(j =0; J < N; J + +)//when i=0, the inner loop executes n when the i=1 inside Loop executes n-1 times ...//so the total number of executions is n+ (n-1) + (n-2) +....+1= (n+1) N/2 { /*The code for this is a sequence of procedural steps with a time complexity of O (1).*/ } }
The total number of executions of the above code is 1+n+n squared +n (n+1)/2=3/2n squared +3/2n+1 So the final time complexity is O (square of N)
Common time complexity
The worst-case run time is a guarantee, which means that the runtime will not run longer than the worst-case scenario.
The average run time is the most meaningful in all cases because it is the desired run time
The spatial complexity of the algorithm
One of the big talk data structures (introduction, algorithm)