[PHP data structure] what exactly do we learn when learning data structures and algorithms?


When it comes to data structures and algorithms, everyone will avoid them. This was originally a professional basic course, but most people didn’t learn it well, let alone a code farmer like me. To tell you the truth, I still admire the programmers who came from the science class, because you only need to review in your daily work or interview, while I learn from scratch. However, fortunately, it’s not too late. Here, we’ll use PHP as sample code to really learn terrible data structures and algorithms from scratch.

data structure

What is a data structure?

Data structure is a collection of data elements with “structure”, which refers to the relationship between data elements

This is Yan Weimin’s definition of data structure in the second edition of data structure. In fact, it is a combination of data. It’s like you go to a bookstore, or a library, or your bookcase. How should these books be placed? The placement form of books is the data structure. You can put them in a mess, or in different categories, or according to your hobbies. You can also put the most commonly used books at hand and the books you don’t often read in the depths of the cabinet. These are data structures.

In the program world, data structure includes two forms, one is logical structure, the other is physical structure.

Logical structure

Even if you haven’t touched data structures at all, as long as you have learned programming, you will have heard more or less of such nouns: set, linear table, tree and graph, which refer to the logical structure in data structures. That is, describing data from logical relationship is a mathematical model abstracted from specific problems. For example, we classify books, with corresponding books under each classification, which is a tree structure. Or we can index the book according to the Pinyin of the book title, and then affix the index label on the bookshelf. This is a hash structure.

Logical structures will be a key point in our whole learning, because various algorithms are the operation implementation of these structures. We will explain this in detail in the following algorithm explanation.

Physical structure

Physical structure is mainly the physical storage of data, also known as storage structure. This is very easy to remember. It has only two forms: sequential storage structure and chain storage structure. Generally, the sequential storage structure is represented by an array, while the chain storage structure is represented by the pointer of the structure in C language, but in PHP, the chain structure will be expressed by classes.

The logical structures mentioned above can be implemented in a sequential or chain way. No matter which way is used, the algorithm operation of the corresponding logical structure can be completed, but different forms or algorithms have different efficiency. Efficiency is the core of the whole data structure and algorithm learning core.


Next, let’s look at what an algorithm is.

The algorithm is a finite instruction set. It accepts some inputs (in some cases, no inputs are required), generates outputs, and must terminate after a finite step

This is Mr. Chen Yue’s definition of algorithm in the second edition of data structure of Zhejiang University Edition. In fact, if we simply understand it, a series of operations for the above data structure are algorithms. For example, if we define a tree, how can we traverse it? This is an algorithm. Traversing a tree includes first order, middle order and second order, and sequence traversal. Which of these methods is good? Which is not good? What kind of physical structure? Linear or chain? These conclusions are based on the execution efficiency of the algorithm. It can be said that there are many kinds of algorithms and their efficiency varies greatly. However, we can’t say that a certain algorithm must be bad. Each algorithm also has its different application scenarios. This is why we want to study the algorithm.

As for the algorithm, we are most concerned about its efficiency, which is defined by time complexity and space complexity.

Time complexity

The time complexity is generally expressed by O (n), which is concerned with the problem scale and statement frequency. Generally, the problem scale is expressed by n (note that this n is unknown. If this n is known, it is a constant order). This n may be a constant (n is exactly equal to) and is written as O (1). This is the best case and can also grow linearly, such as O (n). Of course, there may also be logarithmic or exponential growth, O (logn), O (n ^ 2). Of course, the most important thing is the growth of O (2 ^ n). In this case, you may not see the results of the operation in your lifetime.

We can take a look at a simple piece of code to analyze its time complexity:

echo $a++, PHP_EOL; // O(1)

$n = 10; //  Suppose a quantity is used for testing, and the actual n is unknown. If this known n really occurs in the interview question code, then the algorithm is O (1)
for($i = 0;$i

As can be seen from the above code, the number of nested loops has a great relationship with the growth of N. in addition, the operations inside the loop will also affect the growth. For example, if we are such a piece of code:

$n = 10; //  Let's say a quantity, actually this n is unknown
$m = 3; //  Let's say a quantity, actually this m is unknown
for($i = 0;$i

Then its time complexity is not o (n ^ 2) but o (nm), because our two-layer loops do not operate on the largest n, but n * M. However, if M is large or even equal to N, then the algorithm will become n ^ 2.

In fact, the analysis of time complexity requires some mathematical skills, but for coders like me, if we grasp the loop level and the operation in the loop, we can roughly analyze the time complexity of an algorithm. Of course, for the tricky interview questions of some large factories, it still needs to be decomposed by mathematical methods to obtain the correct time complexity.

In addition, in an algorithm or a function, the largest time complexity shall prevail. At the same time, the best and worst time complexity shall also be considered, because based on the data scale, the time complexity may become worse and worse when the amount of data is large. At this time, we will take the worst time complexity as the final time complexity of this algorithm.

For the problem of time complexity, you can refer to all kinds of algorithm books. Of course, it is best to focus on college textbooks and exercises. You can master it more deeply by doing more questions.

Spatial complexity

Compared with the time complexity, the spatial complexity should be less concerned in the data structure and algorithm, because in most cases, if we only use a third-party variable, the spatial complexity is O (1). If you need to use an array or linked list to implement the algorithm, the spatial complexity of the algorithm is O (n).

Generally, we don’t pay too much attention to spatial complexity, because most algorithms will basically maintain the level of O (1) or O (n). Of course, some algorithms will occupy a very large space complexity, so you can burst your memory every minute. When we encounter such algorithms, we will explain the spatial complexity separately.


The first article is based on theoretical things, which is also the actual situation of learning data structures and algorithms. It must be a combination of theory and practice. Let’s start from here and move towards this bottomless super pit!!

reference material:

Data structure, Second Edition, Yan Weimin

Data structure, Second Edition, Chen Yue

High score notes on data structure, 2020 edition, tianqin postgraduate entrance examination


Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532