Tuesday, 22 January 2013

MAX MIN (DnC) Method


#include<stdio.h>
#include<conio.h>
int a[2222];
int max,min;

int maxmin(int i,int j)
{
int mid;
int maxa,minb;

if (i==j)
{
     max=a[1];
     min=a[1];
}
else
     {
                         if (i==j-1)
                                 {
                                       if(a[i] < a[j])
                                       {
                                                   max=a[j];
                                                   min=a[i];
                                       }
                                       else
                                       {
                                        max=a[i];
                                        min=a[j];
                                       }

                                 }
                                  else
                                   {
                                           mid= (i+j)/2;
                            
                                           maxmin(i,mid);
                                              maxa=max;
                                              minb=min;
                                         maxmin(mid+1,j);
                                                                         if(max < maxa)
                                                              {
                                                                        max = maxa;
                                                                }

                                                                        if(min>minb)
                                                               {
                                                                          min=minb;
                                                                }

                                   }
           }

}

void main()
{
 int i ,num;
clrscr();

printf ("this conining all 3 version of minmax ");
 scanf("%d",&num);

 printf (" enter elements ");
 for (i=1;i<=num;i++)
{
                     scanf("%d",&a[i]);
 }

max=a[0];
min=a[0];

maxmin(1,num);

printf (" \n max =%d",max);
printf (" \n min =%d",min);

getch();
}

No comments:
Write comments

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2016