The Below Program is used to perform searching in array in C programming. It can perform 1.Linear Search. 2.Binary Search. 3.Interpolation Search. The whole program is written in C programming language and is a part of Mumbai University MCA Colleges Data Structure MCA Sem 2
Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void LSearch();
void BSearch();
void ISearch();
int i,j,n,a[20],yn=1,choice,mid,first,last,low,high;
void main()
{
clrscr();
while(choice!=4)
{
printf("\n\n----------------------------- MENU FOR SEARCHING -------------------------------\n\n1.Linear Search.\n\n2.Binary Search.\n\n3.Interpolation Search.\n\n4.Exit.\n\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
LSearch();
break;
case 2:
BSearch();
break;
case 3:
ISearch();
break;
case 4:
exit(1);
default:
printf("\n\nInvalid Choice!!!!!!Try Again");
}
}
getch();
}
void LSearch()
{
printf("\n\n------------------------------- Linear Search ---------------------------------");
printf("\n\nEnter the size of the array : ");
scanf("%d",&n);
printf("\n\nEnter Sorted ARRAY!!!!!!!!!");
printf("\n\nEnter the %d elements : ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nEnter the elements to be searched :");
scanf("%d",&j);
printf("\n\n------------------------------ Original array -------------------------------\n\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]==j)
{
printf("\n\nElement %d is found at location %d\n",j,i+1);
yn=0;
break;
}
}
if(yn==1)
{
printf("\n\nElement %d is not in this array",j);
}
}
void BSearch()
{
printf("\n\n------------------------------- Binary Search --------------------- ------------");
printf("\n\nEnter the size of the array :");
scanf("%d",&n);
printf("\n\nEnter Sorted ARRAY!!!!!!!!!");
printf("\n\nEnter the %d elements :",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nEnter the elements to be searched :");
scanf("%d",&j);
printf("\n\n------------------------------- Original array --------------------- ----------\n\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
first=0;
last= n-1;
while(first<=last)
{
mid=(first+last)/2;
if(j<a[mid])
{
last=mid-1;
}
else if(j>a[mid])
{
first=mid+1;
}
else if(j==a[mid])
{
printf("\n\nElement %d is found at location %d\n",j,mid+1);
yn=0;
break;
}
}
if(yn==1)
{
printf("\n\nElement %d is not in this array",j);
}
}
void ISearch()
{
printf("\n\n---------------------------- Interpolation Search ------------------------------");
printf("\n\nEnter the size of the array :");
scanf("%d",&n);
printf("\n\nEnter Sorted ARRAY!!!!!!!!!");
printf("\n\nEnter the %d elements :",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nEnter the element to be searched :");
scanf("%d",&j);
printf("\n\n------------------------------ Original array -------------------------------\n\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
low = 0;
high = n-1;
while(low<high)
{
mid=low+(high-low)*((j-a[low])/(a[high]-a[low]));
if(j<a[mid])
{
high = mid-1;
}
else if(j>a[mid])
{
low=mid+1;
}
else if(j==a[mid])
{
printf("\n\nElement %d is found at location %d",j,mid+1);
yn=0;
break;
}
}
if(yn==1)
{
printf("\n\nElement is not in this array");
}
}
Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.
No comments:
Post a Comment