-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearSearch.c
51 lines (44 loc) · 906 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
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
clock_t start, end;
int arr[100000];
double cpu_time;
void printArray(int arr[], int n)
{
for(int i = 0; i<n; i++)
printf("%d\t", arr[i]);
printf("\n");
}
void linearSearch(int arr[], int n, int key)
{
for(int i = 0; i<n; i++)
{
if(arr[i]==key)
{
printf("Key found at %d location\n", i+1);
return;
}
}
printf("Key not found\n");
return;
}
int main()
{
int n,i;
printf("Enter the size of array: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
arr[i] = rand()%9001+1000;
}
printArray(arr,n);
int key;
printf("Enter the key: ");
scanf("%d", &key);
start = clock();
linearSearch(arr,n,key);
end = clock();
cpu_time = (double)(end-start)/CLOCKS_PER_SEC;
printf("The time taken is: %lf\n", cpu_time);
}