-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearch.cpp
36 lines (34 loc) · 1.03 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
#include <iostream>
using namespace std;
int binarySearch(int arr[], int leftIndex, int rightIndex, int searchElement)
{
if (rightIndex > 1)
{
int midIndex = leftIndex + ( rightIndex -1) / 2;
if (arr[midIndex] == searchElement)
{
return midIndex ;
}
if (arr[midIndex] < searchElement)
{
return binarySearch(arr,midIndex + 1 ,rightIndex , searchElement);
}
if (arr[midIndex] >searchElement)
return binarySearch(arr,leftIndex ,midIndex -1, searchElement);
}
return -1;
}
int main()
{
int arr[7] = {23, 67, 90, 92, 100, 784, 800}, left, right, searchElement;
left = 0;
right = sizeof(arr) / sizeof(arr[0]);
searchElement = 90;
int position = binarySearch(arr, left, right, searchElement);
if (position == -1){
cout<<"Sorry Element is not present in array"<<endl;
}
else
cout<<"Element is present in index "<<position;
return 0;
}