-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLinear_Search.c
More file actions
52 lines (40 loc) · 1.05 KB
/
Linear_Search.c
File metadata and controls
52 lines (40 loc) · 1.05 KB
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
52
#include <stdio.h>
#include<time.h>
void Linear_Search(int keynum,int num,int array[])
{ int i,found = 0;
for (i = 0; i < num ; i++)
{
if (keynum == array[i] )
{
found = 1;
break;
}
}
if (found == 1)
printf("Element is present in the array at position %d",i+1);
else
printf("Element is not present in the array\n");
}
void main()
{ int num;
int i, keynum, found = 0;
printf("Enter the number of elements ");
scanf("%d", &num);
int array[num];
for ( i=0; i<num; i++)
{
array[i] = (rand() % 100)+1;
}
for ( i=0; i<num; i++)
{
printf(" %d",array[i]);
}
printf("\nEnter the element to be searched ");
scanf("%d", &keynum);
clock_t start,end;
start=clock();
Linear_Search(keynum,num,array);
end=clock();
double time_taken=(((double)(end-start))/CLOCKS_PER_SEC);
printf("\nThis program takes %f seconds to execu",time_taken);
}