/* 2.(a) W.A.P. to search for a given element in a list of size 'n' using
the recursive linear search. Compute the time taken for executing this program. */
#include<stdio.h>
#include<conio.h>
#include<time.h>
clock_t start,finish;
int n;
int LinSearch(int a[],int low,int key)
{
if(low == n) return -1;
else if (key == a[low]) return low;
else return (LinSearch(a,low+1,key));
}
void main()
{
int *a,i,key,pos;
clrscr();
printf("How many elements you wish to enter : ");
scanf("%d",&n);
a = (int *)malloc(n * sizeof(int));
printf("\nEnter %d elements : ",n);
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
printf("\nEnter the key element to be searched : ");
scanf("%d",&key);
start = clock();
pos = LinSearch(a,0,key);
if (pos == -1)
printf("\n\n*** Search Failure ***");
else
{
printf("\n\n*** Search Successful ***");
printf("\nKey element found at position no. %d",pos+1);
}
finish=clock();
printf("\nThe time taken is %.2f",((finish - start) / CLK_TCK));
getch();
}
/* OUTPUT
How many elements you wish to enter : 6
Enter 6 elements : 23 5 56 9 110 417
Enter the key element to be searched : 9
*** Search Successful ***
Key element found at position no. 4
Time taken to search is 0
How many elements you wish to enter : 4
Enter 4 elements : 43 526 76 489
Enter the key element to be searched : 0
*** Search Failure ***
Time taken to search is 0
*/