-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear_Search.cpp
49 lines (46 loc) · 936 Bytes
/
Linear_Search.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
#include <iostream>
using namespace std;
int linearSearch(int arr[], int n, int data)
{
int found = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] == data)
{
return i;
found = 1;
break;
}
}
if (found == 0)
{
return -1;
}
return 0;
}
int main()
{
int n;
// Input Array size
cout << "Input array size: ";
cin >> n;
int arr[n];
// Array Input
cout << "Input array: ";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int data;
// Input searching value
cout << "Input data value: ";
cin >> data;
// call linear search function
int flag = linearSearch(arr, n, data);
if (flag == -1)
cout << "Not Found!" << endl;
else
cout << "Element found at index: " << flag << endl;
main();
return 0;
}