Why learn data structures and algorithms? Do you know the data structure and algorithm?


Why learn data structures and algorithms? Do you know the data structure and algorithm?


Why should we learn data structure and algorithm? Here I give a simple example.

Programming is like a car, and the data structure and algorithm is the internal gearbox of the car. A driver who doesn’t understand the principle of gearbox can also drive. Similarly, a driver who doesn’t understand data structure and algorithm can also program. But if a driver understands the principle of transmission, such as reducing speed to get more traction, or reducing traction to get faster driving speed. Then, when you climb a slope, you can get more traction by using first gear; when you go downhill, you can use low gear to limit the driving speed of the car.

Back to programming, for example, if you want to temporarily store the names of students in a class in memory, what data structure would you choose to store, array, ArrayList, HashSet, or other data structures. If you don’t understand the data structure, you may choose a container to store, and you can complete all the functions. However, with the increase of students’ data in the later stage, there will be performance problems in the randomly selected data structure, and a person who knows the data structure and algorithm will choose the appropriate data structure to solve the corresponding problems in the actual programming, which will greatly improve the process The performance of the order.

data structure

Data structure is the way that computer stores and organizes data. It refers to the collection of data elements with one or more specific relationships.

In general, carefully selected data structure can bring higher operation or storage efficiency. Data structure is often related to efficient retrieval algorithm and index technology.

1. Basic functions of data structure

  • How to insert a new data item
  • How to find a specific data item
  • How to delete a specific data item
  • How to access each data item iteratively for display or other operations

2. Common data structure

Why learn data structures and algorithms? Do you know the data structure and algorithm?

The advantages and disadvantages of these structures are as follows: first have a general impression, later will explain in detail!!!

For arrays, what you call fast search, I think it’s just fast random search, because knowing array subscript, you can get any value by index. But if you want to find a specific value, for an unordered array, you still need to traverse the entire array, so the efficiency of the search is O (n), which is very low (the binary search algorithm for ordered arrays is very fast).

Insert fast, is to insert at the end of the array, get the last index subscript of the array, add 1 to assign value.

Deletion is slow. In addition to the deletion at the beginning and end, the deletion at any middle or front, the following elements need to be translated as a whole, so it is also relatively slow.

To sum up: for arrays, random search is fast, array tail addition and deletion is fast, the rest of the operation efficiency is very low.

Why learn data structures and algorithms? Do you know the data structure and algorithm?


Algorithm is simply a step to solve the problem.

In Java, algorithms are usually implemented by methods of classes. The previous data structures, such as the linked list why insert, delete fast, and find slow, balanced binary tree insert, delete, and find fast, which are caused by the algorithm to achieve these data structures. The sorting implementation we will talk about later is also an important area in the field of algorithm.

1. Five characteristics of the algorithm

  • Poverty: for any input value of combination method, it must be finished after executing the steps, that is, each step in the algorithm can be completed in a limited time.
  • certainty: in each case, the operation that should be performed is specified in the algorithm, so that the executor or reader of the algorithm can understand its meaning and how to perform it. And under any condition, the algorithm has only one execution path.
  • feasibility: all operations in the algorithm must be basic enough, and can be implemented by the basic operations that have been implemented.
  • There is inputAs the quantity value of the object processed by the algorithm, it is usually reflected in a group of variables in the algorithm. Some inputs need to be input in the process of algorithm implementation, while some algorithms may have no inputs on the surface, but they have been embedded in the algorithm.
  • There is outputIt is a group of quantity values with definite relation to “input”, which is the result of information processing by the algorithm. This kind of definite relation is the function of the algorithm.

2. The design principle of the algorithm

  • CorrectnessFirstly, the algorithm should meet the requirements given in a specific way of “rule description”. Secondly, there are four levels to understand whether the algorithm is correct
  1. Program syntax error.
  2. Program for several groups of input data to meet the needs of the results.
  3. The program can meet the requirements of several groups of input data which are carefully selected, typical, harsh and difficult.
  4. The program can get satisfactory results for all legal input data.

PS: generally, the correctness of the third level meaning is used as the standard to measure whether an algorithm is qualified.

  • ReadabilityAlgorithm for human reading and communication, followed by computer execution. Therefore, the algorithm should be easy to understand; on the other hand, the obscure program is easy to hide more errors and difficult to debug.
  • Robustness: when the input data is illegal, the algorithm should react appropriately or deal with it, instead of producing inexplicable output results. Moreover, the way to deal with the error is not to interrupt the program execution, but to return a value indicating the error or the nature of the error, so that it can be handled at a higher level of abstraction.
  • High efficiency and low reserve demandUsually, the efficiency of an algorithm is the execution time of the algorithm, and the storage capacity is the maximum storage space needed in the execution process of the algorithm, both of which are related to the scale of the problem.

The correctness, readability and robustness of the first three points are easy to understand. As for the execution efficiency and storage capacity of the fourth point algorithm, we know that when we compare algorithms, we may say something like “algorithm a is twice as fast as algorithm B”, but in fact, this statement is meaningless. When the number of data items changes, the efficiency ratio of algorithm a and algorithm B will also change. For example, if the number of data items increases by 50%, algorithm a may be three times faster than algorithm B, but if the number of data items decreases by 50%, algorithm a and algorithm B may have the same speed.

So the speed of the algorithm must be related to the number of data items. That is “Big O” representation, which is a relative expression of algorithm complexity. Here I will briefly introduce it, and later I will describe it according to the specific algorithm.

RelativeYou can only compare the same things. You can’t compare an algorithm that does arithmetic multiplication with an algorithm that sorts a list of integers. However, comparing the arithmetic operations of the two algorithms (one for multiplication and one for addition) will tell you something meaningful;

RepresentationBig O (in its simplest form) reduces the comparison between algorithms to a single variable. The choice of this variable is based on observation or hypothesis.

For example, the comparison between sorting algorithms is usually based on the comparison operation (comparing two nodes to determine the relative order of the two nodes). It is assumed that the computation cost of comparison operation is very high. However, what if the computation cost of comparison operation is small and that of exchange operation is large? This changes the previous way of comparison;

Complexity: if it took me one second to sort 10000 elements, how long would it take to sort one million elements? In this case, complexity is a measure of something else.

Then we talk about the storage capacity of the algorithm, including:

Space occupied by the program itself

Space occupied by input data

Space occupied by auxiliary variables

The higher the efficiency of an algorithm, the better, and the lower the storage, the better.


In this article, we briefly introduce the concept of data structure and algorithm, algorithm is the step to solve the problem, and the realization of data structure is inseparable from algorithm, which may be more vague to understand, so don’t worry, we will explain in detail in the specific data structure and algorithm implementation process later.

Write at the end

Welcome to my official account.Calm as a yard】, massive Java related articles and learning materials will be updated in it, and the sorted materials will also be put in it.

If you think the writing is good, just like it and pay attention to it! Focus, don’t get lost, keep updating!!!