Data structure common problem series (1)

1. The difference between array and linked list

   1). From the perspective of the logical structure, the array must have a fixed length, and the data cannot be dynamically increased or decreased, that is, the size of the array cannot be changed once defined. When the data increases, it may exceed the number of originally defined elements; when the data decreases, it causes a waste of memory; the dynamic storage allocation of the linked list can adapt to the dynamic increase and decrease of data, and it is convenient to insert and output data items.
   2). From the perspective of memory storage, the array allocates space from the stack, which is convenient and fast for programmers, but has a small degree of freedom; the linked list allocates space from the heap, with a large degree of freedom, but application management is more troublesome.
   3). From the access method, the array is stored continuously in the memory, so it can be accessed by the subscript index; the linked list is a chain storage structure, when accessing elements, it can only be accessed sequentially from front to back in a linear manner. The visit effect is relatively low.

Keywords: length, storage, access


2. Brief description of quick sort

   1). Select a reference element, usually the first element or the last element.
   2). Divide the records to be sorted into two independent parts through one sorting, and the element values ​​of some of the records are smaller than the reference element value. The element value recorded in the other part is greater than the reference value.
   3). Now the reference element is in the correct position after sorting.
   4). Then use the same method to continue sorting the two records separately until the entire sequence is in order.


3. Comparison of sorting algorithms

time complexity

   1). Square order (O (n 2)) (O(n^{2} ))( O ( n2 ))Sorting: direct insertion, direct selection and bubble sorting;
   2). Linear logarithmic order(O (nlog 2 n)) (O(nlog2n))( O ( n l o g 2 n ) ) sort: quick sort, heap sort, merge sort;
   3). Linear order(O (n)) (O(n))( O ( n ) ) Sorting: radix sorting, in addition to bucket and box sorting;

Description:

   When the original table is ordered or basically ordered, direct insertion sort and bubble sort will greatly reduce the number of comparisons and the number of moving records, and the time complexity can be reduced to O (n) O(n)O ( n )

   The quick sort is the opposite. When the original table is basically ordered, it will degenerate into a bubble sort, and the time complexity will increase to (O (n 2)) (O(n^{2} ))( O ( n2))

   Whether the original table is in order has little effect on the time complexity of simple selection sort, heap sort, merge sort, and cardinal sort.


4. Stability

   If there are multiple records with the same key in the sequence to be sorted, the relative order of these records remains unchanged after sorting, the algorithm is said to be stable; if the relative order of the records changes after sorting, The algorithm is said to be unstable.

   Stable sorting algorithm: bubble sort, insertion sort, merge sort and cardinal sort

   Not a stable sorting algorithm: selection sort, quick sort, hill sort, heap sort


5. Is it more efficient to use loops than recursion?

Both recursion and loop are completely interchangeable. It is not entirely conclusive to say that the efficiency of loops is higher than that of recursion.

1). Recursive algorithm:

Advantages: The code is concise, clear, and easy to verify correctness.

Disadvantages: It requires more function calls to run. If the number of calls is deep, additional stack processing is required (stack overflow may also occur), such as parameter transfer requiring operations such as stacking, which will affect the execution efficiency Have a certain impact. However, for some problems, if recursion is not used, it will be extremely ugly code. After the compiler is optimized, there will be very good efficiency optimization for the function processing of multiple calls, and the efficiency may not be lower than the loop.

2). Loop algorithm:

Advantages: fast speed and simple structure.

Disadvantages: Not all problems can be solved, and some problems are suitable for using recursion instead of loop. If it is not difficult to use loops, it is best to use loops.

6. Methods to resolve hash conflicts

   Hash table (Hash table, also called hash table) is a data structure that is directly accessed based on the key value.

Linear detection method, square detection method, pseudo-random sequence method, zipper method


7. KMP algorithm

   Find in a string whether it contains the matching string of the target.


8. B tree

   According to the characteristics of the type B tree, construct a multi-level type B tree, and then store the relevant information on the nodes as much as possible, and ensure that the number of layers is as small as possible, so that we can find the information faster later. There are fewer /O operations, and the B-type tree is a balanced tree, and the height from each node to the leaf node is the same, which also ensures that each query is stable.

The difference between B tree and B+ tree, take an m-order tree as an example.

1). The number of keywords is different: The branch nodes in the B+ tree have m keywords, and the leaf nodes also have m. The keywords only serve as an index. However, although the B tree also has m sub-nodes, But it only has m-1 keywords.

2). The storage location is different: The data in the B+ tree is stored on the leaf nodes, that is, the data of all the leaf nodes are combined to form complete data, but the data of the B tree is stored in each node. It's not just stored on the leaf nodes.

3). The structure of the branch node is different: The branch node of the B+ tree only stores the keyword information and the pointer of the son (the pointer here refers to the offset of the disk block), which means that the internal node only contains Index information.

4). The query is different: B-tree ends after finding the specific value, while B+ tree needs to find the data in the leaf node through the index. That is to say, the search process of B+ tree goes from the root node. Point the path to the leaf node.

Guess you like

Origin blog.csdn.net/weixin_43283397/article/details/109479562