-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
42 lines (40 loc) · 855 Bytes
/
binary_search.cpp
File metadata and controls
42 lines (40 loc) · 855 Bytes
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
#include <bits/stdc++.h>
using namespace std;
//Time Complexity=O(log(n))
int binary_search(int arr[], int start, int end, int target)
{
int mid = start + (end - start) / 2;
if (start > end)
{
return -1;
}
if (target > arr[mid])
{
return binary_search(arr, mid + 1, end, target);
}
else if (target < arr[mid])
{
return binary_search(arr, start, mid - 1, target);
}
else
{
return mid;
}
}
int main()
{
int array[] = {3, 4, 12, 14, 17, 21, 32, 45, 49};
int el;
cin >> el;
int size = sizeof(array) / sizeof(array[0]);
int index = binary_search(array, 0, size - 1, el);
if (index != -1)
{
cout << array[index] << " found at index:" << index << endl;
}
else
{
cout << "Element not found" << endl;
}
return 0;
}