-
Notifications
You must be signed in to change notification settings - Fork 57
/
Linbin.cpp
90 lines (76 loc) · 2 KB
/
Linbin.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*==============================|-problem statement-|==============================*/
/*
Implement linear search and binary search
*/
/*===================================|-solution-|===================================*/
#include <iostream>
#include <algorithm>
using namespace std;
/*
Linear search traverses through the whole array,
and return the index of the index if the search item matches the arrayth item
*/
int linearSearch(int arr[], int n, int item){
for(int i = 0;i<n;i++){
if(arr[i] == item){
return i;
}
}
return -1;
}
/*
Bineary serach traversels through a sorted by equating mid values with the searched items
and at every turn if the mod does not match it updates update by comparing its value with the searched item.
*/
int binarySearch(int arr[], int n, int item){
int low = 0, high = n -1, mid = (low +high )/2;
while(low<high){
if(arr[low] == item){
return low;
}else if(arr[high] == item){
return high;
}else if(arr[mid] == item){
return mid;
}else if(arr[mid] < item){
low = mid+1;
}
else{
high = mid-1;
}
mid = (high + low)/2;
}
return -1;
}
/*
//implementation of is_sorted (pre defined function available in algorithm)
Boolean isSorted(int arr[]){
for(int i = 1 i<n;i++){
if (a[i] < ar[i-1]){
return 0;
}
}
return 1;
}
*/
int main(int argc, char const *argv[])
{
int n, item;
//the size of the array
cin>>n;
int arr[n];
//inputting the array elements
for(int i =0 ;i<n;i++){
cin>>arr[i];
}
cin>>item;
//searches element using linear search
cout<<linearSearch(arr, n, item);
cout<<"\n";
//searches element using binary search , given that the array is sorted in increasing order;
if(is_sorted(arr, arr+n)){
cout<<binarySearch(arr, n, item);
}else{
cout<<"not sorted in asc order";
}
return 0;
}