forked from mithlesh4257/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLinear_Search.cpp
More file actions
40 lines (32 loc) · 723 Bytes
/
Linear_Search.cpp
File metadata and controls
40 lines (32 loc) · 723 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
#include <iostream>
using namespace std;
// Function for linear search
int Linear_Search(int array[], int size, int desired)
{
for(int i = 0; i < size; i++)
{
// return position if element is found
if(array[i] == desired)
return i;
}
return -1;
}
// Driver Function
int main()
{
int array[] = {2, 4, 6, 7, 3, 1, 5};
// Element 4 to be searched
if(Linear_Search(array, 7, 4) != -1)
cout << "Found" << endl;
else
cout << "Not Found" << endl;
//Element 9 to be searched
if(Linear_Search(array, 7, 9) != -1)
cout << "Found" << endl;
else
cout << "Not Found" << endl;
return 0;
}
// Output
// Found
// Not Found