Data Structures and Algorithms - Overview of Data Structures and Algorithms

1.1 What is a data structure

Official explanation:
Data structure is a subject that studies the operating objects in programming problems of non-numerical computing, as well as the relationship and operation between them and other related issues.

Vernacular:
Data structure is a collection of data elements organized according to a certain relationship, used to organize and store data

1.2 Data structure classification

Traditionally, we can divide data structures into two categories: logical structures and physical structures.

Logical structure classification:
Logical structure is a model abstracted from specific problems. It is a structure in an abstract sense. It is classified according to the relationship between data elements in objects. It is also a problem that we need to pay attention to and discuss in the following topics.

a. Set structure: In addition to belonging to the same set, the data elements in the set structure have no other relationship between them.
insert image description here
b. Linear structure: There is a one-to-one relationship between data elements in a linear structure

insert image description here
c. Tree structure: There is a one-to-many hierarchical relationship between data elements in the tree structure
insert image description here

d. Graphical structure: The data elements of the graphical structure are many-to-many relationships.
insert image description here
Physical structure classification:
The real representation of the logical structure in the computer (also known as the image) is called the physical structure, and it can also be called the storage structure. Common physical structures include sequential storage structure and chain storage structure.

Sequential storage structure:
Put data elements into storage units with continuous addresses, and the logical and physical relationships between the data are consistent. For example, our commonly used arrays are sequential storage structures.

insert image description here
There are certain disadvantages in the sequential storage structure, just like in life, when queuing up, some people may jump in the queue or some people may leave suddenly under special circumstances. At this time, the entire structure is changing, and a chained storage structure is needed at this time.

Chain storage structure:
store data elements in any storage unit, and this group of storage units can be continuous or discontinuous. At this time, the logical relationship between the data elements cannot be reflected between the elements, so a pointer is introduced in the chained storage structure to store the address of the data element, so that the location of the associated data element can be found through the address;
insert image description here

1.3 What is an algorithm

Official explanation:
Algorithm refers to an accurate and complete description of a problem-solving solution, a series of clear instructions for solving problems, and an algorithm represents a strategy
mechanism for solving problems in a systematic way. That is to say, the required output can be obtained within a limited time for a certain standard input.

Vernacular:
According to certain conditions, calculate some data to get the required results.

1.4 Initial experience of the algorithm

In life, if we encounter a certain problem, often the solution is not the only one.

For example, how to go from Xi'an to Beijing? There will be different solutions. We can take a plane, a train, a car, or even walk. The time cost and money cost of different solutions are different. For example, it takes the least time to fly, but The cost is the highest, and the walk is the cheapest, but takes the longest.

Another example is how to pay for a courtyard house within the Second Ring Road in Beijing? There will also be different solutions, which can be a one-time cash payment or a mortgage through the bank. The costs brought by these two solutions are also different. One-time payment, although the money paid at that time was high and the pressure was high, there was no interest. The total interest for 30 years is almost double the loan amount, which is an overpayment.

In the program, we can also use different algorithms to solve the same problem, and the cost of different algorithms is also different. In general, an excellent algorithm pursues the following two goals:

  1. Spend the least amount of time to complete the requirements;
  2. Occupy the least memory space to complete the requirements;

Below we use some practical cases to experience some algorithms.

Requirement 1:
Calculate the sum from 1 to 100.
The first solution:

public static void main(String[] args) {
    
    
	int sum = 0;
	int n=100;
	for (int i = 1; i <= n; i++) {
    
    
		sum += i;
	}
	System.out.println("sum=" + sum);
}

The second solution:

public static void main(String[] args) {
    
    
	int sum = 0;
	int n=100;
	sum = (n+1)*n/2;
	System.out.println("sum="+sum);
}

The first solution needs to complete the following actions:

  1. Define two integer variables;
  2. Perform 100 addition operations;
  3. Print the result to the console;

The second solution needs to complete the following actions:

  1. Define two integer variables;
  2. Perform 1 addition operation, 1 multiplication operation, and 1 division operation, for a total of 3 operations;
  3. Print the result to the console;

Obviously, the second algorithm completes the requirements and takes less time.

Requirement 2:
Calculate the factorial of 10
The first solution:

public class Test {
    
    
	public static void main(String[] args) {
    
    
		//测试,计算10的阶乘
		long result = fun1(10);
		System.out.println(result);
	}
	//计算n的阶乘
	public static long fun1(long n){
    
    
		if (n==1){
    
    
		return 1;
		}
		return n*fun1(n-1);
	}
}

The second solution:

public class Test {
    
    
	public static void main(String[] args) {
    
    
		//测试,计算10的阶乘
		long result = fun2(10);
		System.out.println(result);
	}
	//计算n的阶乘
	public static long fun2(long n){
    
    
		int result=1;
		for (long i = 1; i <= n; i++) {
    
    
			result*=i;
		}
		return result;
	}
}

The first solution, using recursion to complete the requirements, the fun1 method will be executed 10 times, and the first execution is not completed, the second execution is called, the second execution is not completed, the third execution is called... Finally, at the most time, It is necessary to open up 10 blocks of memory in the stack memory to execute 10 fun1 methods respectively.

The second solution is to use the for loop to complete the requirements. The fun2 method will only be executed once. In the end, you only need to open up a piece of memory in the stack memory to execute the fun2 method.

Obviously, the second algorithm fulfills the requirements and takes up less memory space.

Guess you like

Origin blog.csdn.net/qq_33417321/article/details/121944884