
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Binary Search using pthread in C Program?
We know that the binary search approach is one of the most suitable and effective sorting algorithm. This works on sorted sequence. The algorithm is simple, it simply finds the element from middle, then divide the list by two parts, and moves either towards the left sublist, or right sublist.
We know the algorithm of it. Now we will see how to use binary search technique in multithreading environment. The number of threads depends on number of cores are present in the system. Let us see the code to get the idea.
Example
#include <iostream> #define MAX 16 #define MAX_THREAD 4 using namespace std; //place arr, key and other variables as global to access from different thread int arr[] = { 1, 6, 8, 11, 13, 14, 15, 19, 21, 23, 26, 28, 31, 65, 108, 220 }; int key = 31; bool found = false; int part = 0; void* binary_search(void* arg) { // There are four threads, each will take 1/4th part of the list int thread_part = part++; int mid; int start = thread_part * (MAX / 4); //set start and end using the thread part int end = (thread_part + 1) * (MAX / 4); // search for the key until low < high // or key is found in any portion of array while (start < end && !found) { //if some other thread has got the element, it will stop mid = (end - start) / 2 + start; if (arr[mid] == key) { found = true; break; } else if (arr[mid] > key) end = mid - 1; else start = mid + 1; } } 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); //wait, to join with the main thread if (found) cout << key << " found in array" << endl; else cout << key << " not found in array" << endl; }
Output
31 found in array
- Related Articles
- Binary Search in C++ program?
- Java Program to search ArrayList Element using Binary Search
- C++ Program to Find Maximum Element in an Array using Binary Search
- Binary Search (Recursive and Iterative) in C Program
- C++ Program to Implement a Binary Search Tree using Linked Lists
- C++ Program to Perform Uniform Binary Search
- Binary Search for Rational Numbers without using floating point arithmetic in C program
- Binary Search program in JavaScript
- C++ Program to Search for an Element in a Binary Search Tree
- Binary search in C#
- Binary Search in C++
- C++ Program to Find the maximum subarray sum using Binary Search approach
- Python Program to Sort using a Binary Search Tree
- C++ Program to Implement Randomized Binary Search Tree
- C++ Program to Compare Binary and Sequential Search

Advertisements