-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
49 lines (42 loc) · 939 Bytes
/
Copy pathbinary_search.cpp
File metadata and controls
49 lines (42 loc) · 939 Bytes
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
/*Again doing it for ADA lab ..This one is at least better than linear search */
#include<iostream>
#include<math.h>
#define MAX 100
using namespace std ;
int main ()
{
int arr[MAX] , i , n , search , low , mid , high , ans = -1;
cout << "Enter the number of elements in the array\t" ;
cin >> n;
cout << "Enter the elements of the array\n" ;
for(i = 0; i<n ; i++){
cin >> arr[i] ;
}
cout << "Enter the element to be searched\t" ;
cin >> search ;
low = 0 ;
high = n -1;
while(low <= high){
mid = (low + high)/2 ;
if(arr[mid] == search){
ans = mid ;
break ;
}
else if(arr[mid] < search){
//recurse on right subtree
low = mid + 1 ;
}
else if (arr[mid] > search){
//recurse on left subtree
high = mid -1 ;
}
}
cout << ans <<endl;
if (ans>=0){
cout << "The number is located at the index " << ans << endl;
}
else {
cout << "Element not found\n" ;
}
return 0 ;
}