Binary search is a widely used searching algorithm that requires the array to be sorted before search is applied. The main idea behind this algorithm is to keep dividing the array in half (divide and conquer) until the element is found, or all the elements are exhausted. 二分搜索是搜索算法中广泛使用的,搜索之前需要把数组排序好。主要思想是不断把数组分半(分而治之),直到找到特定的元素,或是找不到。
It works by comparing the middle item of the array with our target, if it matches, it returns true otherwise if the middle term is greater than the target, the search is performed in the left sub-array.找数组的中间值和目标值对比。如果相等返回True,如果中间值比目标值大,进行左边子数组的搜索。如果中间值比目标值小,进行右边子数组的搜索。
If the middle term is less than target, the search is performed in the right sub-array.
The prototype for binary search is :
binary_search(startaddress, endaddress, valuetofind) startaddress: the address of the first element of the array. endaddress: the address of the last element of the array. valuetofind: the target value which we have to search for.
// CPP program to implement
// Binary Search in
// Standard Template Library (STL)
#include
#include
using namespace std;
void show(int a[], int arraysize)
{
for (int i = 0; i < arraysize; ++i)
cout << a[i] << " ";
}
int main()
{
int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int asize = sizeof(a) / sizeof(a[0]);
cout << "\n The array is : ";
show(a, asize);
cout << "\n\nLet's say we want to search for 2 in the array";
cout << "\n So, we first sort the array";
sort(a, a + asize);
cout << "\n\n The array after sorting is : ";
show(a, asize);
cout << "\n\nNow, we do the binary search";
if (binary_search(a, a + 10, 2))
cout << "\nElement found in the array";
else
cout << "\nElement not found in the array";
cout << "\n\nNow, say we want to search for 10";
if (binary_search(a, a + 10, 10))
cout << "\nElement found in the array";
else
cout << "\nElement not found in the array";
return 0;
}
The output of the above program is :
The array is : 1 5 8 9 0 6 7 3 4 2 0 Let's say we want to search for 2 in the array So, we first sort the array The array after sorting is : 0 1 2 3 4 5 6 7 8 9 Now, we do the binary search Element found in the array Now, say we want to search for 10 Element not found in the array
Related Article: std::bsearch in C++
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Sorting is one of the most basic functions applied to data. It means arranging the data in a particular fashion, which can be increasing or decreasing. There is a builtin function in C++ STL by the name of sort().排序是对数据进行处理的基本方式,对数据进行排序,升序或降序,
Internally this function is implemented as Quick-sort. The complexity of it is O(N*log(N)).实现的方法快速排序,时间复杂度O(N*log(N))
The prototype for sort is :
sort(startaddress, endaddress) startaddress: the address of the first element of the array endaddress: the address of the last element of the array
#include
#include
using namespace std;
void show(int a[])
{
for(int i = 0; i < 10; ++i)
cout << a[i] << " ";
}
int main()
{
int a[10]= {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
cout << "\n The array before sorting is : ";
show(a);
sort(a, a+10);
cout << "\n\n The array after sorting is : ";
show(a);
return 0;
}
The outut of the above program is :
The array before sorting is : 1 5 8 9 6 7 3 4 2 0 The array after sorting is : 0 1 2 3 4 5 6 7 8 9
Refer std::sort() for more details.