File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+ int array[15 ],i,x,n;
4
+ int BinarySearch (int low , int high)
5
+ {
6
+ if (low >high)
7
+ {
8
+ return -1 ;
9
+ }
10
+ else
11
+ {
12
+ int mid = (low+high)/2 ;
13
+ if (array[mid]==x)
14
+ {
15
+ return mid;
16
+ }
17
+ else if (array[mid]>x)
18
+ {
19
+ return BinarySearch (low, n-1 );
20
+ }
21
+ else
22
+ {
23
+ return BinarySearch (mid+1 ,high);
24
+ }
25
+ }
26
+ }
27
+ int main ()
28
+ {
29
+ cout<<" Input the size of the element: " ;
30
+ cin>> n;
31
+ cout<<" Input the value of the array : " ;
32
+ for (i=0 ; i<n;i++)
33
+ {
34
+ cin>>array[i];
35
+ }
36
+ cout<<" Input the value you want to search : " ;
37
+ cin>>x;
38
+ int result = BinarySearch (0 , n - 1 );
39
+ if (result == -1 ) {
40
+ cout << " The value is not found in the array." ;
41
+ } else {
42
+ cout << " The value is found at index: " << result;
43
+ }
44
+ return 0 ;
45
+ }
You can’t perform that action at this time.
0 commit comments