-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_search.cpp
More file actions
35 lines (28 loc) · 785 Bytes
/
linear_search.cpp
File metadata and controls
35 lines (28 loc) · 785 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
#include <bits/stdc++.h>
using namespace std;
int linearSearch(int a[], int n, int num) {
int i;
for (i = 0; i < n; i++)
if (a[i] == num)
return i;
return -1;
}
int main() {
clock_t start, endc;
int n, num, a[10000];
for(int i = 0; i < 10000; i++)
a[i] = i;
cout << "Enter number to be found: ";
cin >> num;
start = clock();
int position = linearSearch(a, 10000, num);
if (position == -1)
cout << "Not found \n";
else
cout << "Number found at index " << position << endl;
endc = clock();
double time_taken = (endc - start) / double(CLOCKS_PER_SEC);
cout << "Time taken by program is : " << fixed << time_taken << setprecision(5);
cout << " sec " << endl;
return 0;
}