Minimum And Maximum using Divide and Conquer > C Program
Analysis of Algorithm
Minimum And Maximum using divide and conquer
#include<conio.h>
void minmax(int i,int j,int *min, int *max);
int a[20];
void minmax(int i,int j,int *min, int *max)
{
int lmin, rmin, lmax, rmax, mid;
if(i==j)
{
*min = a[i];
*max = a[j];
}
else if(j==i+1)
{
if(a[i]>a[j])
{
*max = a[i];
*min = a[j];
}
else
{
*max = a[j];
*min = a[i];
}
}
else
{
mid = (i+j)/2;
minmax(i,mid,&lmin,&lmax);
minmax(mid+1,j,&rmin,&rmax);
*min = (lmin<rmin)?lmin:rmin;
*max = (lmax>rmax)?lmax:rmax;
}
}
void main()
{
int n, min, max, i;
clrscr();
printf("ENTER THE NUMBER OF ELEMENTS");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("ENTER ELEMENT") ;
scanf("%d",&a[i]);
}
minmax(1,n,&min,&max);
printf("MINIMUM VALUE IS %d AND MAXIMUM VALUE IS %d",min,max);
getch();
}
/*
Comments
Post a Comment