[C language inspector training camp ninth day] Overview of the data structure associated with C language and 408

foreword

C language and data structure are inseparable, and data structure is the top priority of 408 computer professional courses. Through the structure and pointer of C language, the logical storage in data structure can be easily and vividly realized. Today's blog It will initially introduce the logical structure and storage structure, time complexity and space complexity in the data structure. Although both are basic concepts, they are also frequently tested.
insert image description here
insert image description here
insert image description here
Time complexity is almost a must for every big question!

1. Logical structure and storage structure

It can be seen from the figure below that the logical structure is abstract (that is, artificially defined according to certain rules), and the storage structure is concrete (what is what). Below are several common logical
insert image description here
structures, which are just as shown in the figure below in terms of storage logic As shown, but when storing in the actual computer, methods such as malloc dynamic memory application are still used to implement chained storage.

insert image description here
The figure below shows several storage structures, among which sequential storage and chained storage are the two most commonly used methods in the future.
insert image description here
The implementation of sequential storage is shown in the figure below: In memory, each node is connected to each other, and can be quickly indexed by subscript.
insert image description here
The way of chain storage is shown in the figure below: the actual memory addresses of the nodes are not necessarily adjacent, and the nodes record the addresses of the nodes related to them. Only sequential indexing is possible, not quick indexing.
insert image description here
In general, the logical structure and storage structure can be summarized as the following figure:
insert image description here
the difference between the two is shown in the figure below:
insert image description here

Let me talk about the concepts of index storage and hash storage here. The two will be introduced in detail in the data structure course later, and they are only for understanding at present.

  • Index storage , which stores data elements and storage methods of relationships between elements. All storage nodes are stored in one area. Another index area is set to store the relationship between nodes. An index area consists of several index entries. If each storage node has an index entry in the index table, the index table is called "dense index"; if a group of storage nodes only corresponds to one index entry in the index table, the index table is called "sparse index". index".
  • Hash storage : Hash storage, also known as hash storage, 是一种力图将数据元素的存储位置与关键码之间建立确定对应关系的查找技术. The basic idea of ​​hash storage is: the storage address of the node is determined by the key code value of the node. In addition to being used for lookup, hash technology can also be used for storage. Hash is a development of array storage method. Compared with array, the data access speed of hash is higher than that of array, because the storage location of data in the array can be found according to the part of the stored data, and then the data can be accessed quickly. , the ideal hash access speed is very fast, unlike the traversal process in the array, which uses some elements that store the contents of the array as the input of the mapping function, and the output of the mapping function is the location of the stored data, such access speed The implementation of traversing the array is omitted, so the time complexity can be considered as O (1), while the time complexity of array traversal is O (n).

Generally, the structure of hash storage is shown in the following figure:
insert image description here

The following is a real question about hash storage. When looking for elements in the hash table, you only need to calculate according to the hash function and then search according to the characteristics of the hash table, which is much faster than looking for elements in the array.
insert image description here

2. Time complexity

Time complexity and space complexity are often used as one of the most important criteria to test whether an algorithm is good or not.
insert image description here
Basic concept of time complexity:

insert image description here
Time Complexity, Efficiency Comparison Curve
insert image description here
The following are some common occurrence scenarios of time complexity.
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

insert image description here

Thinking question: If the number of executions of an algorithm is 3n3+5n, what is the time complexity of the algorithm?


Answer: It is o (n3), because the time complexity calculation ignores the coefficients of high-order items and low-order items

3. Space complexity

Space complexity S(n) refers to the size of the auxiliary space used during the operation of the algorithm. Write it as:
s(n)=O(f(n))

  • In addition to the instructions, constants, variables, and input data that need to store the algorithm itself, it also needs to store the storage unit that operates on the data.
  • If the space occupied by the input data only depends on the problem itself and has nothing to do with the algorithm, then it is only necessary to analyze the auxiliary units required for the implementation of the algorithm.
  • The algorithm works in place means that the auxiliary space required by the algorithm is constant, that is, O(1).
    Later, we will combine specific codes to explore what is space complexity and what is time complexity.

insert image description here

Guess you like

Origin blog.csdn.net/apple_51931783/article/details/129893812