-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
50 lines (38 loc) · 1.13 KB
/
binary_search.cpp
File metadata and controls
50 lines (38 loc) · 1.13 KB
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
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int a[], int n, int num) {
int low = 0, high = n;
int mid =(low + high - 1) / 2;
while(high >= low)
{
if (a[mid] == num)
return mid;
else if (a[mid] > num)
{
high = mid - 1;
}
else
low = mid + 1;
mid = (low + high - 1) / 2;
}
return -1;
}
int main() {
clock_t start, endc;
int n, num, a[10000];
for(int i = 0; i < 10000; i++)
a[i] = i;
cout << "Enter number to be found: ";
cin >> num;
start = clock();
int position = binarySearch(a, 10000, num);
if (position == -1)
cout << "Not found \n";
else
cout << "Number found at index " << position << endl;
endc = clock();
double time_taken = (endc - start) / double(CLOCKS_PER_SEC);
cout << "Time taken by program is : " << fixed << time_taken << setprecision(5);
cout << " sec " << endl;
return 0;
}