-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_binarySearch.cpp
66 lines (58 loc) · 1.39 KB
/
06_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
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;
void fillArray(int arr[],int n){
cout<<"Fill the elements in the array.Elements must be in sorted order..."<<endl<<endl;
cout<<"0: ";
cin>>arr[0];
for(int i=1; i<n; i++){
cout<<i<<": ";
cin>>arr[i];
if(arr[i-1]>arr[i]){
cout<<"Value not sorted.....Enter the value again but sorted..."<<endl;
i--;
}
}
}
void displayArray(int arr[],int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
int binarySearch(int arr[],int n){
int item;
cout<<"Enter the element to search in the array: ";
cin>>item;
int start = 0;
int end = n-1;
int mid = start + (end-start)/2;
while(start<=end){
if(arr[mid]==item){
return mid;
}
else if(arr[mid]>item){
end = mid-1;
}
else{
start = mid+1;
}
mid = start + (end-start)/2;
}
return -1;
}
int main(){
int arr[8];
int n = 8;
fillArray(arr,n);
cout<<endl<<"The Input Array looks like...."<<endl;
displayArray(arr,n);
cout<<endl;
int res = binarySearch(arr,n);
cout<<endl;
if(res==-1){
cout<<"Element not found...."<<endl;
}
else{
cout<<"Element found at Index: "<<res<<endl;
}
return 0;
}