forked from pujazha/Hactoberfest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.cpp
55 lines (48 loc) · 1.12 KB
/
BinarySearch.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
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;
int binary(int arr[], int size, int element) //function to implement binary search
{
int low = 0; // Initialize low to the first index of the array.
int high = size - 1; // Initialize high to the last index of the array.
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == element)
{
return mid;
}
else if (arr[mid] < element)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
int main()
{
int arr[100], size, element, i;
cout << "Enter the size of the array = ";
cin >> size;
for (int i = 0; i < size; i++)
{
cout << "Enter element no. " << i + 1 << endl;
cin >> arr[i];
}
cout << "Entered array is : " << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] <<" ";
}
cout<<endl;
cout << "Enter the element that you want to search for = " << endl;
cin >> element;
// int size = sizeof(arr) / sizeof(arr[0]); // Use arr[0] to get the size of an element
// int element = 82;
int searchindex = binary(arr, size, element); // calling the binary() function
cout << "Location = " << searchindex << endl;
return 0;
}