-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathBinary Search using pthread
69 lines (51 loc) · 1.28 KB
/
Binary Search using pthread
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 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;
}