forked from 790hanu/Annex-qr-code-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_search.cpp
38 lines (32 loc) · 806 Bytes
/
fast_search.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
#include <iostream>
using namespace std;
int binarySearch(int arr[], int start, int end, int ele)
{
if (end >= start) {
int mid = (start+end)/2;
if (arr[mid] == ele)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main(void)
{
int n;
cout<<"Enter the size of Array: ";
cin>>n;
int A[n];
cout<<" Enter the Array: ";
for(int i=0;i<n;i++)
cin>> A[i];
int ele;
cout<<"Enter the element you wants to search: ";
cin>>ele;
int result = binarySearch(arr, 0, n - 1, ele);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}