-
Notifications
You must be signed in to change notification settings - Fork 1
/
linearSearch.c
42 lines (36 loc) · 891 Bytes
/
linearSearch.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int linearSearch(int arr[], int len, int elem);
int main()
{
srand(time(0));
clock_t start, end;
double time_taken;
int total_items=500000;
int arr[total_items], elem;
for (int i = 0; i < total_items; i++)
{
arr[i] = (rand()%10)+i;
}
puts("Enter an element to search");
scanf("%d", &elem);
start = clock();
linearSearch(arr, total_items, elem);
end = clock();
time_taken = (double)(end - start) / CLOCKS_PER_SEC;
printf("Time taken: %fs", time_taken);
}
int linearSearch(int arr[], int len, int elem)
{
int found = -1, j;
for (j = 0; j < len; j++)
{
if (arr[j] == elem)
{
found = j;
break;
}
}
(found == -1) ? (puts("No match found!\n")) : (printf("Match found at index %d\n", found));
}