Binary search is a popular method of searching in a sorted array or list. It simply divides the list into two halves and discard the half which has zero probability of having the key. On dividing we check the mid point for the key and uses the lower half if key is less than mid point and upper half if key is greater than mid point. Binary search has time complexity of O(log(n)).
Binary search can also be implemented using multi-threading where we utilizes the cores of processor by providing each thread a portion of list to search for the key.
Number of threads depends upon the number of cores your processor has and its better to create one thread for each core.
Input : list = 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 key = 7 Output : 7 found in list Input : list = 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 key = 111 Output : 111 not found in list
Note – It is advised to execute the program in Linux based system.
Compile in linux using following code:
g++ -pthread program_name.cpp
110 found in array
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Meta Binary Search | One-Sided Binary Search
- Why is Binary Search preferred over Ternary Search?
- Interpolation search vs Binary search
- Linear Search vs Binary Search
- Binary Search
- Binary Search in PHP
- Binary Search in Java
- Variants of Binary Search
- Uniform Binary Search
- Binary Search a String
- The Ubiquitous Binary Search | Set 1
- Binary Search (bisect) in Python
- Randomized Binary Search Algorithm
- Complexity Analysis of Binary Search
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.