-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryLinear.cpp
More file actions
59 lines (50 loc) · 1.13 KB
/
binaryLinear.cpp
File metadata and controls
59 lines (50 loc) · 1.13 KB
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
#include <bits/stdc++.h>
using namespace std;
const int MAX=1e5;
int linear_search(int arr[], int n, int element)
{
int position = 0;
int i = 0;
while((arr[i]!=element) && (i < n))
{
position++;
i++;
}
if(i < n) return position;
else return -1;
}
int binary_search(int arr[], int n, int element)
{
sort(arr, arr+n);
int low = 0, high = n - 1;
while(low <= high)
{
int mid = low + (high - low)/2;
if(arr[mid] == element) return mid;
else if(arr[mid] > element) high = mid - 1;
else low = mid + 1;
}
return -1;
}
int main() {
int t;cin>>t;
while(t--)
{
int n;cin>>n;
int arr[MAX];
for(int i=0;i<n;i++) cin>>arr[i];
int element;
cout<<"Enter the element which you want to search:"<<endl;
cin>>element;
int pos = linear_search(arr, n, element);
//Assuming position starting from 1
if (pos == -1) cout<<"Not Found"<<endl;
else
cout<<"Found, Using Linear Search at Position:"<<pos + 1<<endl;
//Assuming position starting from 1
pos = binary_search(arr, n, element);
if (pos == -1) cout<<"Not Found"<<endl;
cout<<"Found, Using Binary Search at Position:"<<pos + 1<<endl;
}
return 0;
}