first, the concept
Data structure is like a catalyst, if there is no raw material is useless, only with the algorithm can help the algorithm to achieve the task faster;
Data structure: Refers to the existence of one or more specific relationships between the set of elements of a dataset, which is simply a collection of various relationships between data.
Program Design = data structure + algorithm;
Explanation: If you want to write a program, you need to choose a good data structure, coupled with good problem-solving algorithm;
second, the noun explanation
Data is the most extensive concept, data can have multiple data objects, data objects can have multiple data elements, data elements can have multiple data items;
Data: Symbols that can be entered into a computer and can be processed by a computer;
Data objects: a class of things;
Data elements: a record;
Data item: The attribute of a record;
Like what:
Class person{ String name; int age; } The data object is person, the data element is an instance of person, such as person A,person B, the data item is name, age attribute; |
Abstract data type (ADT): Analogy to a class, custom type;
third, the data structure introduction
Data structure: The relationship between the elements of a database, the above example, is the relationship between A and B;
The data structure is divided into:
(1) Logical structure: the relationships between data elements, such as the relationship between A and B;
-Collection structure: There is no relationship between data elements;
-Linear structure: a one-to-one relationship between data elements, such as linked lists;
-Tree structure: a one-to-many relationship between data elements, such as a two-fork tree;
-Graphical construction: Many-to-many relationships between data elements, such as non-aligned graphs and a direction graph;
(2) Physical structure: The storage relationship of data elements in the computer;
-Sequential storage: Data elements are stored continuously, and data is stored in contiguous memory;
-linked list storage: Data elements are connected by linked lists, and storage is discontinuous;
I. The relationship between algorithms and data structures
The algorithm and data structure are inseparable, because if the data structure is a static thing, if not to apply it, it is just a very dead thing, so we need algorithm;
such as the adjacency table, only to introduce him may just understand his concept, but if we learn Dfs, BFS know his purpose;
second, the algorithm concept
Algorithm: The algorithm is the description of solving the specific problem, and the finite sequence of the instruction;
There may be multiple algorithms for solving a problem, but a good algorithm is unique;
Space complexity and time complexity need to be weighed;
Characteristics of the algorithm (basic requirements of the algorithm)
(1) Input and output: The algorithm needs to have input and output;
(2) Poor sex: After the execution of a limited time, the limited time here refers to the time you expect;
(3) Certainty: Under the same premise of input, the output should be unique;
(4) Feasibility: Able to run the results;
Design requirements for algorithms (ideal for algorithms)
(1) Correctness: For the legal input can get the correct answer, for illegal input can get illegal hint;
(2) Readability: Easy to read, others can easily read;
(3) Robustness: For illegal input, need to get enough hints, not anomalies;
(4) Fast execution speed, less storage space;
methods of measuring algorithm efficiency
(1) Post-mortem method: Write a good algorithm after testing, generally not, because the external environment will greatly affect the speed of the algorithm, such as the quality of the machine, the size of the input;
(2) Prior statistical method: before the preparation of a good algorithm before estimating;
determine which efficiency of f (n) and g (n) is high: if n is present so that n>n, f (n) >g (n), then f (n) is inferior to g (n);
The worst case in the algorithm is a bottom line, generally we need to ensure that the worst case is less than a certain time; large o notation
The large O notation is obtained by removing the addition constant, preserving the highest order, and removing the constant of the highest order, given the running time;
e.g. 4n^2+3n+2 = O (n^2)
Gaussian summation algorithm
1+2+3+.....+99+100 by:
sum = 1 + 2 + 3 +.....+99+100
sum = 100+99+98 +.......+2 + 1
2*sum = 101*100
sum = 5050