-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSearch.cpp
43 lines (42 loc) · 857 Bytes
/
BSearch.cpp
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
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <omp.h>
using namespace std;
#define n 100
int binarySearch(int a[], int target)
{
int low=0, high=n, mid;
bool abort = false;
#pragma omp parallel
while(low<=high && !abort)
{
mid=(low+high)/2;
if(a[mid]<target)
low=mid+1;
else if(a[mid]>target)
high=mid-1;
else
abort = true;
}
if(abort)
return mid;
return -1;
}
int main()
{
int a[n];
srand(time(NULL));
for(int i=0; i<n; i++)
a[i] = rand()%1000;
sort(a, a+n);
cout<<"Your sorted array is: ";
for(int i=0; i<n; i++)
cout<<a[i]<<" ";
cout<<"\nEnter the number you want to find: ";
int target;
cin>>target;
(binarySearch(a, target) == -1)?cout<<"Key not found":cout<<"Key found at index "<<binarySearch(a, target)<<endl;
return 0;
}