Maximum and Minimum

#include<stdio.h>

int max,min,a[50];

void maxmin(int i,int j)
{
    int mid,max1,min1;
    if(i==j)
    {
        max=min=a[i];
    }
    else if(i==j-1)
    {
        if(a[i]<a[j])
        {
            min=a[i];
            max=a[j];
        }
        else
        {
            max=a[i];
            min=a[j];
        }
    }
    else
    {
        mid=(i+j)/2;
        maxmin(i,mid);
        max1=max;
        min1=min;
        maxmin(mid+1,j);
        if(max1>max)
            max=max1;
        if(min>min1)
            min=min1;
    }
}

void main()
{
    int n,i;
    printf("Enter number of elements ");
    scanf("%d",&n);
    printf("Enter elements ");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    maxmin(0,n-1);
    printf("Max %d, Min %d\n",max,min);
}

/*Output
Enter number of elements 5
Enter elements 65 32 11 8 95
Max 95, Min 8
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s