Sunday, May 5, 2013

Write C programs that use both recursive and non recursive functions to perform the following searching operations for a Key value in a given list of integers : i) Linear search ii) Binary search


Write C programs that use both recursive and non recursive functions to perform the following searching operations for a Key value in a given list of integers : 
i) Linear search ii) Binary search

/*Write a C program Linear Search Using Recursive Functions*/
#include<stdio.h>
#include<conio.h>45
int lin_search(int[],int,int);
main()
{
    int a[100],n,i,ele;
    clrscr();
    printf("Enter Size");
    scanf("%d",&n);
    printf("Enter %d elements",n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    printf("The array elements");
    for(i=0;i<n;i++)
    printf("%4d",a[i]);
    printf("\nEnter element to search");
    scanf("%d",&ele);
    i=lin_search(a,n-1,ele);
    if(i!=-1)
    printf("\nThe element %d found at %d location",ele,i+1);
    else
    printf("\nThe element %d is not found",ele);
    getch();
}
int lin_search(int x[100],int n,int ele)
{
    if(n<=0)
    return -1;
    if(x[n]==ele)
    return n;
    else
    return lin_search(x,n-1,ele);
}
 
output:

Enter Size5
Enter 5 elements25
30
12
54
60
The array elements 
25  30  12  54  60
Enter element to search 
60

The element 60 found at 5 location

1 comments:

Sir the ans of the question is wrong...you have write the question as recursive and non recursive function in linear and binary search..but you have done only recursive in linear seaech

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More