0% found this document useful (0 votes)
47 views4 pages

Daa Lab Program-01

The document outlines a program for implementing the Linear Search algorithm in C, including the generation of random elements and the search for a specified key. It details the process of measuring the time taken for searches with varying numbers of elements, providing sample output data. A graph is included to visually represent the relationship between the number of elements and the time taken for the search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views4 pages

Daa Lab Program-01

The document outlines a program for implementing the Linear Search algorithm in C, including the generation of random elements and the search for a specified key. It details the process of measuring the time taken for searches with varying numbers of elements, providing sample output data. A graph is included to visually represent the relationship between the number of elements and the time taken for the search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ALGORITHMS LAB BCA SEM-IV (NEP)

PROGRAM – 01
Write a program to implement LINEAR SEARCH algorithm. Repeat the experiment for different

values of n, the number of elements in the list to be searched and plot a graph of the time taken

versus n.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int Linear_Search(int*, int, int);

void main()
{
int i, n, KEY;
int repeat=1000000, result;
int *a = malloc(n*sizeof(int));
double Time_taken;
clock_t start, stop;
printf("\t\t\t **--**LINEAR SEARCH**--**\n\n");
printf("Enter the number of elements:\t");
scanf("%d",&n);
printf("\n\n The Elements in the array:\n");

Department of BCA VEIT DEGREE COLLEGE Page 1


ALGORITHMS LAB BCA SEM-IV (NEP)

for(i=0;i<n;i++)
{
a[i]=rand();
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\nEnter the KEY Element to be searched:\t");
scanf("%d",&KEY);
start=clock();
for(i=0;i<repeat;i++)
{
result=Linear_Search(a,n,KEY);
}
stop=clock();
if(result==-1)
{
printf("\nKey Element %d not found",KEY);
}
else
{
printf("\nKey Element %d found at position %d.",KEY,result);
}
Time_taken=((double)(stop-start))/CLK_TCK*1000;
printf("\nTime Taken to search a Key Element %d is %f
milliseconds.",KEY,Time_taken);
Department of BCA VEIT DEGREE COLLEGE Page 2
ALGORITHMS LAB BCA SEM-IV (NEP)

//FUNCTION::>Linear Search
int Linear_Search(int *a,int n,int KEY)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==KEY)
{
return i;
}
}
return -1;
}

OUTPUT:
n Time (ms)
5 47
10 94
15 141
20 249

Department of BCA VEIT DEGREE COLLEGE Page 3


ALGORITHMS LAB BCA SEM-IV (NEP)

GRAPH PLOT:

LINEAR SEARCH
300
249
250
Time Taken (ms)

200
141
150
94
100
47
50

0
5 10 15 20
n ELEMENTS

Department of BCA VEIT DEGREE COLLEGE Page 4

You might also like