Home > Article > Backend Development > How to use search algorithms in C++
How to use the search algorithm in C
The search algorithm is a very important algorithm in computer science. It is used to find specific elements in a data set. . C language provides many built-in search algorithms, such as linear search, binary search, etc. This article describes how to use search algorithms in C and provides specific code examples.
1. Linear search
Linear search is a simple and direct search algorithm. Its principle is to compare the element to be found with each element in the data set one by one until a matching element is found. Or loop through the entire data collection.
C provides several linear search algorithms, the most commonly used of which is the find function. The following is a sample code for linear search using the find function:
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> data = {1, 2, 3, 4, 5}; int target = 3; auto result = std::find(data.begin(), data.end(), target); if (result != data.end()) { std::cout << "找到了目标元素 " << target << std::endl; } else { std::cout << "未找到目标元素 " << target << std::endl; } return 0; }
In the above code, a vector container data is first created, which contains some integer elements. Then a target element target is defined, and the find function is used to perform a linear search in the data container. If the target element is found, output "Target element found", otherwise output "Target element not found".
2. Binary search
Binary search is an efficient search algorithm, and its premise is that the data set is already ordered. The principle is to first divide the data set into two parts, and then judge the size relationship between the target element and the middle element to determine which part the target element is in, and then perform a binary search in this part until the target element is found or the binary cannot be continued.
C provides functions such as binary search algorithm lower_bound and upper_bound. The following is an example code for binary search using the lower_bound function:
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> data = {1, 2, 3, 4, 5}; int target = 3; auto result = std::lower_bound(data.begin(), data.end(), target); if (result != data.end() && *result == target) { std::cout << "找到了目标元素 " << target << std::endl; } else { std::cout << "未找到目标元素 " << target << std::endl; } return 0; }
In the above code, a vector container data is also first created and a target element target is defined. Then use the lower_bound function to perform a binary search in the data container. If the target element is found and the element pointed to by the result pointer is equal to the target element, then "target element found" is output, otherwise "target element not found" is output.
It should be noted that the binary search algorithm requires that the data set is already ordered. Therefore, the data must be sorted before using the binary search algorithm.
To sum up, this article introduces how to use the search algorithm in C and provides specific code examples. These search algorithms are very useful in practical programming and can help us find specific elements efficiently. At the same time, by understanding the principles and usage of search algorithms, we can improve our programming capabilities and be able to better solve practical problems.
The above is the detailed content of How to use search algorithms in C++. For more information, please follow other related articles on the PHP Chinese website!