**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.

Examples:

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

`// CPP Program to perform binary search using pthreads ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// size of array ` `#define MAX 16 ` ` ` `// maximum number of threads ` `#define MAX_THREAD 4 ` ` ` `// array to be searched ` `int` `a[] = { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 }; ` ` ` `// key that needs to be searched ` `int` `key = 110; ` `bool` `found = ` `false` `; ` `int` `part = 0; ` ` ` `void` `* binary_search(` `void` `* arg) ` `{ ` ` ` ` ` `// Each thread checks 1/4 of the array for the key ` ` ` `int` `thread_part = part++; ` ` ` `int` `mid; ` ` ` ` ` `int` `low = thread_part * (MAX / 4); ` ` ` `int` `high = (thread_part + 1) * (MAX / 4); ` ` ` ` ` `// search for the key until low < high ` ` ` `// or key is found in any portion of array ` ` ` `while` `(low < high && !found) { ` ` ` ` ` `// normal iterative binary search algorithm ` ` ` `mid = (high - low) / 2 + low; ` ` ` ` ` `if` `(a[mid] == key) { ` ` ` `found = ` `true` `; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `else` `if` `(a[mid] > key) ` ` ` `high = mid - 1; ` ` ` `else` ` ` `low = mid + 1; ` ` ` `} ` `} ` ` ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `pthread_t threads[MAX_THREAD]; ` ` ` ` ` `for` `(` `int` `i = 0; i < MAX_THREAD; i++) ` ` ` `pthread_create(&threads[i], NULL, binary_search, (` `void` `*)NULL); ` ` ` ` ` `for` `(` `int` `i = 0; i < MAX_THREAD; i++) ` ` ` `pthread_join(threads[i], NULL); ` ` ` ` ` `// key found in array ` ` ` `if` `(found) ` ` ` `cout << key << ` `" found in array"` `<< endl; ` ` ` ` ` `// key not found in array ` ` ` `else` ` ` `cout << key << ` `" not found in array"` `<< endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

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.

## Recommended Posts:

- 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)
- Best First Search (Informed Search)
- Repeatedly search an element by doubling it after every successful 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 contribute@geeksforgeeks.org. 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.