Sunday, April 16, 2017

Design and Analysis of algorithm BBIT makaut

Design and Analysis of algorithm BBIT makaut


WRITE A PROGRAM FOR LINEAR SEARCH
SOURCE CODE
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,a[20];
int num,pos,f=0;
printf(" enter the number of elements ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" a[%d] =",i);
scanf("%d",&a[i]);
}
printf(" enter number to be searched");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
f=1;
pos=i;
printf(" %d is found at pos=%d",num,i);
break;
}
}
if(f==0)
{
printf(" %d does not exist in the array");
}
getch();
}


OUTPUT
enter the number of elements
5
a[0] =5
a[1] =2
a[2] =3
a[3] =4
a[4] =9
enter number to be searched4
4 is found at pos=3












WRITE A PROGRAM FOR BINARY SEARCH
SOURCE CODE
#include<stdio.h>
#include<conio.h>
int main()
{ int i,n,a[20];
int num,pos=-1,f=0;
int b,m,e;
printf(" enter the number of elements ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" a[%d] =",i);
scanf("%d",&a[i]);
}
printf(" enter number to be searched");
scanf("%d",&num);
b=0;
e=n-1;
while(b<=e)
{
m=(b+e)/2;
if(a[m]==num)
{
printf(" %d is found at pos=%d",num,m);
f=1;
break;
}
if(a[m]>num)
e=m-1;
else if(a[m]<num)
b=m+1;
}
if(b>e&&f==0)
printf(" %d doesnot exist in the array");
getch();
return 0;
}
OUTPUT
enter the number of elements 5
a[0] =2
a[1] =3
a[2] =5
a[3] =4
a[4] =1
enter number to be searched5
5 is found at pos=2






WRITE A PROGRAM TO SORT ELEMENTS USING BUBBLE SORT
SOURCE CODE
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,a[20];
int j,t;
printf(" enter the number of elements ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" a[%d] =",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf(" the array in sorted in acsending order");
for(i=0;i<n;i++)
{ printf(" a[%d]=%d",i,a[i]);
}
getch();
return 0;
}
OUTPUT
enter the number of elements
5
a[0] =26
a[1] =12
a[2] =35
a[3] =98
a[4] =2
the array in sorted in acsending order
a[0]=2
a[1]=12
a[2]=26
a[3]=35
a[4]=98




WRITE A PROGRAM TO SORT ELEMENTS USING MERGE SORT
SOURCE CODE
#include<stdio.h>
#include<conio.h>
void merge(int a[],int b,int mid,int e);
void mergesort(int a[],int b ,int e );
int main()
{
int a[10],i,n,j,k;
printf(" enter the number of elements in array ");
scanf("%d",&n);
printf(" enter the elements of the array");
for(i=0;i<n;i++)
{
printf(" a[%d]=",i);
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
printf(" the sorted array is: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);

No comments:

Post a Comment