[Data structure] 1. Data structure concept and complexity analysis

1. What is a data structure?

"A data structure is the various relationships between a data object, an instance of the object, and the data elements that make up the instance, and this relationship can be given by defining related functions." - via "Data" Structures, Algorithms for Applications". A simple understanding is the relationship between data that can be represented in a structured manner.

"A data structure is a physical implementation of the abstract data type ADT." - via Analysis of Data Structures and Algorithms. It can be seen that the process of structured data is the process of data structure realization.

Let's analyze some of the concepts:

Data: Data is the carrier of information, it should be able to be recognized, processed and stored by a computer. Every day, numbers, characters, images, videos, sounds, etc. are all data. Data Element: A data element is the basic unit of data. Data item: A data item is a field, attribute, etc. in a data element. For example, we regard a database table as a piece of data, then the data element is each record in it, and the data item is each field.

So in a nutshell, a data structure is a collection of representations of relationships between data.

2. What is an algorithm?

Algorithm is a set of methods used to operate programs and solve problems. It can usually be used to solve a class of problems. It needs to meet five characteristics:

Inputs: Zero or more inputs.

Output: One or more outputs.

Finiteness: Finished in acceptable time after finite steps.

Deterministic: Each step has a definite meaning without ambiguity.

Feasibility: Every step is feasible.

3. Complexity Analysis

We usually use time complexity and space complexity to measure the pros and cons of an algorithm.

  • Time complexity: used to evaluate the time consumed by executing the program, which can estimate the degree of processor usage by the program;
  • Space complexity: It is used to evaluate the memory space occupied by the executing program, and it can estimate the usage of the computer memory by the program.

Usually we mainly analyze the time complexity of the program. The time complexity is best, worst, and average. We generally focus on the worst case.

Time complexity analysis: Big O notation O ( 1 ) Constant order < O ( logn ) Logarithmic order < O ( n ) Linear order < O ( nlogn ) Linear logarithmic order < O ( n 2 ) Square order < O ( 2 n ) Exponential order O(1) Constant order < O(log_n) Logarithmic order < O(n) Linear order < O(nlog_n) Linear order < O(n^2) Square order < O(2^n ) exponential orderO ( 1 ) constant order<O(logn) logarithmic _ _<O ( n ) linear order<O ( n l o gn) linear logarithmic order _<O ( n2 )Squareorder<O(2n )exponentialorder

It can be seen from the figure below that as the number continues to increase, the complexity changes more and more.

function

Tips: To analyze the time complexity, we only need to find the minimum execution statement of the program and analyze its execution times, usually focusing on the loop body; time complexity means, we generally take the highest order as the final complexity, all constants The orders are all taken as 1, without coefficients.

Analysis example:

//T(n)=O(1)
int i=1;
int s = i+1; 

Even if there are many variables, the number of executions of each statement is 1, and we focus on the smallest execution unit.

//T(n)=O(logn),log2n一般以logn表示
int i =1;
while(i < n){
    
    
    i = i * 2; //此时1*2*2*2*...=n;即log2n个2相乘
}
//T(n)=O(n)
int s = 0;
for(i = 1;i <n;i++){
    
    
	s += i;
}
//T(n)=O(nlogn)
for(int i = 1;i < n;i++){
    
    
	int j = 0;
	while(j<n){
    
    
		j = j * 2;
	}
}

The inner judgment is executed logn times, and the outer loop is executed n times; that is, nlogn

//T(n)=O(n^2)
int k = 0;
for(int i = 0;i < n;i++){
    
    
	for(int j = 0;j < n;j++){
    
    
		k = i+j;
	}
}

Data structure is the soul of a program, and the use of data structures and algorithms must be involved in large and complex systems.
In general, program = data structure + algorithm.

Logical structure classification of data structures:

  • Linear structure
  • nonlinear structure

Among them, linear structures can be divided into sequential storage structures and chained storage structures: including arrays, queues, linked lists, and stacks;
nonlinear structures: multidimensional arrays, generalized tables, tree structures, and graph structures.

Guess you like

Origin blog.csdn.net/qq_40589204/article/details/118605878