Skip to content

Commit 322680c

Browse files
Add files via upload
1 parent 4dbf6ad commit 322680c

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

05_twoWayLinearSearch.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
using namespace std;
3+
//function to fill the empty array with elements
4+
void fillArray(int arr[],int n){
5+
for(int i=0; i<n; i++){
6+
cout<<"arr["<<i<<"]: ";
7+
cin>>arr[i];
8+
}
9+
}
10+
// function to print the given input array
11+
void displayArray(int arr[],int n){
12+
for(int i=0; i<n; i++){
13+
cout<<arr[i]<<" ";
14+
}
15+
}
16+
// function for two way linear search
17+
void twoWayLinearSearch(int arr[],int n){
18+
int item;
19+
cout<<"Enter the element to search in the array: ";
20+
cin>>item;
21+
22+
int start = 0;
23+
int end = n-1;
24+
int flag = 0;
25+
26+
while(start<=end){
27+
if(arr[start] == item){
28+
flag = 1;
29+
break;
30+
}
31+
else if( arr[end] == item){
32+
flag = 2;
33+
break;
34+
}
35+
start++;
36+
end--;
37+
}
38+
if(flag==1){
39+
cout<<"Element found at index: "<<start<<endl;
40+
}
41+
else if(flag==2){
42+
cout<<"Element found at index: "<<end<<endl;
43+
}
44+
else{
45+
cout<<"Sorry!! Element not found..."<<endl;
46+
}
47+
}
48+
int main(){
49+
// Here in two way linear search, we use two pointers as start and end and check whether element at either start or end is equal to the item.If is it so, we return the index of the particular element else we return that element not found
50+
int n = 8;
51+
int arr[8];
52+
53+
fillArray(arr,n);
54+
cout<<endl;
55+
cout<<"The Given Input Array looks like..."<<endl;
56+
displayArray(arr,n);
57+
58+
cout<<endl;
59+
twoWayLinearSearch(arr,n);
60+
cout<<endl;
61+
62+
return 0;
63+
}

05_twoWayLinearSearch.exe

45.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)