Chapter One Introduction
1. Basic concepts and terminology:
- Data: the information carrier, is described objective things attribute number, and all the characters can be entered into a computer and a computer program to recognize and process the set of symbols
- Data object: a collection of data elements with the same nature, which is a subset of data
- Data element: the basic unit of data , usually considered and processed as a whole
- Data items: an integral component of the data elements a minimum unit
Data contains data objects, and data elements constitute data objects.
The identity information of the owner can be used as a data object, the identity information of each individual can be used as a data element, and the name and number of the identity information can be used as a data item.
1.1 data
Data type (collection + operation)
- Atomic type: value collection + operation (int, char, float, etc.)
- Structure type: a collection of structures + operations (list, map, set, etc.)
- Abstract data type ADT: Abstract data type Abstract data type (ADT) refers to one
数学模型
and the one defined on the model一组操作
. The definition of an abstract data type only depends on its set of logical characteristics, and not how it is represented in the computer实现无关
. Usually use (数据对象、数据关系、基本操作集
) such triples to represent abstract data types.
1.2 Structure
In any problem, data elements do not exist in isolation, but there is a certain relationship between them, this kind of data elements are structured with each other 关系称为结构
. A data structure is a collection of data elements that have one or more specific relationships with each other. The data structure comprises three aspects: 逻辑结构、存储结构和数据的运算
. The logical structure and storage structure of data are two inseparable aspects. An algorithm 设计
depends on the selected logical structure, and the algorithm 实现
depends on the storage structure used.
1.3 Data structure
The data structure is the presence of one or more particular between mutual relationship between the data elements of the set.
1.4 Three elements of data structure
- Logical structure
- Physical structure (storage structure)
- Data calculation
1.5 Logical structure
逻辑结构
Refers to the 逻辑关系
logical relationship between data elements to describe the data. It 存储无关
is independent of the computer in relation to the data .
The logical structure of the data is divided into 线性结构
and非线性结构
集合
There is no other relationship between the data elements in the structure except the relationship of "belonging to the same set". Similar to mathematical sets线性结构
The only一对一
relationship between the data elements in the structure . Such as queuing树形结构
There is a one-to-many relationship between the data elements in the structure. Family tree图状结构或网状结构
There is a many-to-many relationship between the data elements in the structure. Such as a map
1.6 Physical structure (storage structure)
The storage structure refers to the representation of the data structure in the computer (also known as the image), also known as the physical structure. It includes the representation of data elements and the representation of relationships. The storage structure of data is the realization of the logical structure in computer language, which depends on the computer language. Data storage structure are: 顺序存储、链式存储、索引存储和散列存储
.
- Sequential storage: The physical locations of storage are adjacent. (ps. The location is the location of the information in the computer.)
- Linked storage: The physical locations of storage are
未必
adjacent, and adjacent elements can be found by recording the physical locations of adjacent elements. - Index storage: similar to a directory, query through an index.
- Hash storage: directly calculate the physical address of the element through the keyword (detailed later)
Sequential storage: The physical locations of storage are adjacent.
Linked storage: the physical locations of storage are 未必
adjacent
1.7 Calculation of data
Operations include operations defined and implemented , operations defined for the logical structure , implemented for the operation of the physical structure .
Example: Taking people as an example, suppose there is an operation to calculate the appearance of people.
Face value = the sum of the beauty of the facial features (the definition of the calculation is based on the logical structure)
reads the information of each person's facial features through the computer, and then adds the facial value. (The realization of the operation is based on the physical structure)
2. Algorithm & Algorithm Evaluation
2.1 Algorithm
Algorithm: A description of solving a specific problem. It is a finite sequence of instructions, each of which represents one or more operations.
2.2 Algorithm characteristics
- Finiteness: An algorithm must
有穷步
end after execution , and each step of the operation is completed in finite time. - Feasibility: An algorithm must be feasible, and the operations described in the algorithm can be achieved.
- Determinism: Every instruction and every sentence in the algorithm must have a definite meaning, and the same input must get the same output.
- Input: An algorithm must have zero or more inputs.
- Output: An algorithm must have zero or more outputs.
2.2 Algorithm VS Program
Algorithm (instructor) : A method or process to solve a problem, considering how to convert input into output, a problem can have many algorithms.
Program (implementer) : A program is a specific realization of an algorithm in a certain design language.
Finite nature : the algorithm must be finite, the program can be infinite.
Correctness : The algorithm must be correct, the program can be wrong.
Description method : The algorithm can be described in pseudo code, programming language, etc. The program can only be written and run in programming language.
2.3 Measurement of algorithm efficiency
时间复杂度
:
- It is used to measure the speed of the algorithm execution time as the problem scale increases. ·
- Time complexity is a function of problem size: denoted as T(n), time complexity mainly analyzes the order of T(n)
- T(n)=O(f(n)), Big O notation, f(n) is the frequency of basic operations in the algorithm, generally we consider
最坏情况
the time complexity.
计算方法
: Take 最快
the function item that the algorithm time increases ,把它的系数改为1
Basic operation frequency: the time complexity of the deepest loop execution.
Common time complexity
O(1)<O(log~2~^n^)<O(n)<O(nlog~2~^n^)<O(n^2^)<O(n^3^)<O(2^n^)<O(n!)<O(n^n^)
空间复杂度:
- It is used to measure the speed of the increase in the space required by the algorithm as the problem scale increases:
- Is a function of problem size: S(n)=0(g(n))
2.4 Time complexity
How to calculate the complexity
int sum=0; //执行1次
for(inti=0;i<=n;i++){
//int i=0执行一次,i<n执行n+1次,i++执行n+1次
sum=sum+i;
}
Time analysis: The algorithm executed 3n+6 sentences.
Assuming that the execution time of each statement is the same, it is a constant t. Then the total time t=(3n+6)*t
As the problem size n increases, the growth rate of the total time is consistent with the growth rate of n, so the complexity is O(n).
Conclusion:
- Complexity is about
增长率
, so it can be straightforward忽视常数项,系数化为1
. - Generally, you can directly pay attention to
循环段基本操作语句
(in the examplesum=sum+i
)执行次数
.
Time complexity calculation ( 单个循环体
)
直接关注循环体的执行次数
, set to k
Time calculation complexity ( 多个循环体
)
two calculation rules: 乘法规则,加法规则
.
2.5 Space complexity
The space complexity S(n) refers to 辅助空间
the size used in the operation of the algorithm , denoted as: S(n)=(f(n))
- Auxiliary space: In addition to storing algorithm
本身
instructions, constants, variables and input data, it also needs to store storage units for data operations. - Algorithm
原地工作
means that the auxiliary space required by the algorithm is constant, namely O(1). - Common is O(1), O(n) is more
Welcome to public concern number 理木客
more exciting waiting for you to discover