Binary Search using pthread
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