forked from rachitsainii/cpp-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.cpp
43 lines (40 loc) · 1005 Bytes
/
binary.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 <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
int binary_search(int arr[], int first, int last, int key)
{
if (last >= first) {
int mid = first + (last - first) / 2;
if (arr[mid] == key)
return mid;
if (arr[mid] > key)
return binary_search(arr, first, mid - 1, key);
return binary_search(arr, mid + 1, last, key);
}
return -1;
}
int main()
{
int n, mid, last, first=0, key, flag=0, result;
cout<<"Enter Number of Elements: ";
cin>>last;
int arr[last];
cout<<"Enter elements in Sorted order: ";
for(int i=0;i<last;i++){
cin>>arr[i];
}
cout<<"Enter Key: ";
cin>>key;
auto start = steady_clock::now();
result = binary_search(arr, first, last, key);
if (result != -1){
cout<<"Key Found at index: "<<result<<endl;
}
else{
cout<<"Key not Found"<<endl;
}
auto stop = steady_clock::now();
auto duration = duration_cast<nanoseconds>(stop - start);
cout << "\nTime taken by function: "<< duration.count() << " nanoseconds" << endl;
}