-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_linear.cpp
More file actions
53 lines (43 loc) · 881 Bytes
/
search_linear.cpp
File metadata and controls
53 lines (43 loc) · 881 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
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
int linear_search(int *arr,int n,int x){
for(int i=0;i<n;i++){
if(arr[i]==x) return i;
}
return -1; // not able to find
}
int binary_search(int *arr,int n,int x){
int l=0,r=n-1;
while(l<=r){
int mid=l+(r-l)/2;
if(arr[mid]==x){
return mid;
} else if(arr[mid]>x){
r=mid-1;
} else{
l=mid+1;
}
}
return -1;
}
int main(){
int arr_size,ele_to_find;
cin>>arr_size;
int arr[arr_size];
for(int i=0;i<arr_size;i++) cin>>arr[i];
cin>>ele_to_find;
// linear search
int ind;
ind=linear_search(arr,arr_size,ele_to_find);
int bin_ind=binary_search(arr,arr_size,ele_to_find);
if(ind!=-1)
cout<<"Linear Search "<<ind+1<<endl; // from 1 to n
else
cout<<-1;
if(bin_ind!=-1){
cout<<"bin "<<bin_ind+1;
} else{
cout<<-1;
}
return 0;
}