[Project Combat] Introduction to Search Algorithms

1. What is the search algorithm?

A lookup algorithm is a method of finding a specific element in a data structure.
A search algorithm is an algorithm for finding a particular element in a data structure.
The performance of a lookup algorithm depends on the nature of the data structure and the efficiency of the lookup.

In practical applications, it is often necessary to find data satisfying certain conditions from a large amount of data, so choosing an appropriate search algorithm has a great influence on the efficiency of the program.

Choosing an appropriate search algorithm can improve search efficiency, and it is necessary to select an appropriate algorithm according to the data structure and specific application scenarios

Different lookup algorithms are suitable for different data structures and scenarios. In practical applications, it is necessary to select an appropriate search algorithm according to the nature of the data structure and search efficiency.

2. Common search algorithms

The following are several common lookup algorithms:

2.1 Linear search (linear table search, sequential search)

Linear search is the simplest search algorithm
Sequential search is a simple search algorithm

It compares one by one from the first element of the data until the target element is found or all elements are traversed.
It starts with the first element of the data structure and compares each element in turn until the target element is found or the search ends.

The time complexity of linear search is O(n), where n is the size of the data.
The time complexity of sequential lookup is O(n), where n is the length of the data structure.
Sequential search is suitable for small data structures or sorted data structures.

2.2 Binary search

Binary search is an algorithm to find a specific element in an ordered array.
Binary search is an algorithm for finding elements in an ordered data structure.

Binary search is suitable for ordered data structures, such as binary search trees, ordered arrays, etc.

The time complexity of binary search is O(logn), where n is the length of the data structure.

It divides the data structure into two parts, and then determines which part to look for based on the position of the target element. It first divides the array into two parts, compares the size relationship between the middle element and the target element, returns if the middle element is equal to the target element, otherwise continues to search in the corresponding half according to the size relationship between the middle element and the target element.

2.3 Hash lookup (hash table lookup)

Hash lookup is an algorithm that uses a hash function to map a key to an index position of an array, and searches directly at that position.

A hash table is a data structure that maps keys to values.
Hash tables use a hash function to map keys to locations to make lookups more efficient.
The time complexity of lookup and insertion of a hash table is usually O(1).
The time complexity of hash lookup is O(1),

The hash table needs to resolve the hash collision problem, it needs extra space to store the hash table and possible collision resolution.

2.4 Index lookup

Index lookup is an algorithm that uses index tables and data tables for lookups.
It first searches according to the keywords in the index table, finds the corresponding index, and then searches according to the index in the data table.

The time complexity of index lookup depends on the structure and size of the index table and data table.

2.5 Number structure search (binary search tree)

A binary search tree is a data structure where
each node of a binary search tree has two child nodes,

  • The value of the left child node is less than the value of the parent node
  • The value of the right child node is greater than the value of the parent node.

The binary search tree can improve the efficiency of search, and the time complexity of binary search is O(logn).
The time complexity of inserting and deleting elements in a binary search tree is O(logn).

Guess you like

Origin blog.csdn.net/wstever/article/details/129927264
Recommended