this program is to find maximum and minimum using divide and conquer technique
int min=32000,max=0;
void maxi(int [],int,int),merge(int [],int,int,int,int);
void main()
{
int a[30],n,i;
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the aray elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
maxi(a,0,n-1);
printf("maximum is %d and minimum is %d\n",max,min);
getch();
}
void maxi(int a[],int first,int last)
{
int mid=(first+last)/2;
if(first+1<last)
{
maxi(a,first,mid);
maxi(a,mid+1,last);
merge(a,first,mid,mid+1,last);
}
}
void merge(int a[],int f1,int l1,int f2,int l2)
{
int m1,m2,mi1,mi2;
/* max1=a[f1];
min1=a[f1];
for(i=f1+1;i<l1;i++)
{
if(max1<a[i])
max1=a[i];
else if(min1>a[i])
min1=a[i];
}*/
if(a[f1]>a[l1])
{
m1=a[f1];
mi1=a[l1];
}
else
{
m1=a[l1];
mi1=a[f1];
}
if(a[f2]>a[l2])
{
m2=a[f2];
mi2=a[l2];
}
else
{
m2=a[l2];
mi2=a[f2];
}
if(m1>max || m2>max)
{
if(m1<m2)
max=m2;
else
max=m1;
}
if(mi1<min || mi2<min)
{
if(mi1<mi2)
min=mi1;
else
min=mi2;
}
}
THE PROGRAM IS CAPABLE TO FIND THE MAXIMUM AND MINIMUM BUT IS THIS FOLLOWS CORRECT ALGORITHM OF DIVIDE AND CONQUER???.Usually divide and conquer technique provides us a complexity less than usual technique.. can anyone explain how d above code s efficient than d usual code to find the maximum and minimum