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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Meta Binary Search | One-Sided Binary Search
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Linear Search vs Binary Search
- Sublist Search (Search a linked list in another list)
- Repeatedly search an element by doubling it after every successful search
- Best First Search (Informed Search)
- Longest Common Prefix using Binary Search
- Finding minimum vertex cover size of a graph using binary search
- Binary Search for Rational Numbers without using floating point arithmetic
- Find square root of number upto given precision using binary search
- Calculating n-th real root using binary search
- Create a Sorted Array Using Binary Search
- Longest substring with K unique characters using Binary Search
- Check if an array is sorted and rotated using Binary Search
- Check if a given number is a Perfect square using Binary Search
- Number Guessing Game in Python using Binary Search
- Longest substring consisting of vowels using Binary Search
- Floor value Kth root of a number using Recursive Binary Search
- Binary Search Visualization using Pygame in Python
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.