Skip to content

Commit e9146c9

Browse files
authored
Update 01) Binary Search.cpp
1 parent 455a989 commit e9146c9

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
1-
# include <iostream>
1+
#include <iostream>
22
using namespace std;
3-
int binarySearch(int arr[], int n, int key){
4-
int s = 0 ,e = n-1;
5-
6-
while(s <= e){
7-
int mid = s + (e-s)/2;
8-
9-
if( arr[mid] == key){
10-
return mid;
11-
}
12-
13-
if( key > arr[mid]){
14-
s = mid+1;
15-
}
16-
17-
else{
18-
e = mid-1;
3+
int binarySearch (int arr[], int n, int key)
4+
{
5+
int s = 0, e = n - 1;
6+
while (s <= e)
7+
{
8+
int mid = s + (e - s) / 2;
9+
if (arr[mid] == key)
10+
{
11+
return mid;
12+
}
13+
if (key > arr[mid])
14+
{
15+
s = mid + 1;
16+
}
17+
else
18+
{
19+
e = mid - 1;
20+
}
21+
mid = s + (e - s) / 2;
22+
}
23+
return -1;
1924
}
2025

21-
mid = s + (e-s)/2;
22-
}
23-
return -1;
24-
}
25-
26-
int main() {
27-
28-
int arr[] = {1, 2, 4, 5, 7, 9, 11};
29-
int n = 7;
30-
31-
int ans = binarySearch(arr, 7, 2);
32-
if(ans == -1)
33-
cout<<"Not Found ";
34-
else
35-
cout<<" Element found at index " << ans;
26+
int main ()
27+
{
28+
int arr[] = { 1, 2, 4, 5, 7, 9, 11 };
29+
int n = 7;
30+
int ans = binarySearch (arr, 7, 2);
31+
if (ans == -1)
32+
cout << "Not Found ";
33+
else
34+
cout << " Element found at index " << ans;
3635
}

0 commit comments

Comments
 (0)