-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Search_Function.cpp
More file actions
60 lines (47 loc) · 1.04 KB
/
Binary_Search_Function.cpp
File metadata and controls
60 lines (47 loc) · 1.04 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
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;
int Binary_search(int arr[], int element, int n)
{
int start = 0,end = n-1, mid;
while (start <= end)
{
mid = (start + end) / 2;
if (arr[mid] == element)
{
cout << "The Element " << element << " is Found at index : " <<mid<<"\n";
break;
}
else if (arr[mid] > element)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
if (start > end)
{
cout << "Element" << element << " is Not Found \n";
}
return 0;
}
int main()
{
int arr[10], i, element, n;
n = sizeof(arr) / sizeof(int);
cout << "Enter the elements for Searching:\n ";
for (i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "HERE ARE THE ELEMENTS: ";
for (i = 0; i < n; i++)
{
cout << "\t" << arr[i];
}
cout << "\nEnter Element to Search : ";
cin>>element;
Binary_search(arr, element,n);
return 0;
}