Tuesday, 14 May 2013

9. MINIMUM-MAXIMUM USING DIVIDE AND CONQUER


Code:
import java.util.*;
class MinMax
{           private int x[],n;
public MinMax()
            {
                        Scanner s =new Scanner(System.in);
                        System.out.println("\n\tEnter the no of elements");
                        n=s.nextInt();
                        x=new int[n];
                        for(int i=0;i<n;i++)
                        {
                                    System.out.println("\n\tEnter the element at position:"+""+(i+1));
                                    x[i]=s.nextInt();
                        }
            }
                        public int min(int a,int b)
                        {
                                    if(a<b)
                                    return a;
                                    else
                                    return b;
                        }
                        public int max(int a,int b)
                        {
                                    if(a>b)
                                    return a;
                                    else return b;
                        }
                        public int getNum()
                        {
                                    return n;
                        }
                        public void minmax(int i,int j,int []fminmax)
                        {
                                    if(i==j)
                                    {
                                                fminmax [0]=fminmax[1]=x[i];
                                                return;
                                    }
                                    if(i==j-1)
                                    {
                                                fminmax [0]=min(x[i],x[j]);
                                                fminmax [1]=max(x[i],x[j]);
                                                return ;
                                    }
                        int []gminmax=new int[2];
                        int []hminmax=new int[2];
                        int m=(i+j)/2;
                        minmax(i,m,gminmax);
                        minmax(m+1,j,hminmax);
                        fminmax [0]=min(gminmax [0],hminmax [0]);
                        fminmax [1]=max(gminmax [1],hminmax [1]);
                        return;
                        }
           
}
class MinMaxExp
{
                        public static void main(String []arg)
                        {
                                    MinMax obj=new MinMax();
                                    int zminmax[]=new int[2];
                                    obj.minmax(0,obj.getNum()-1,zminmax);
                                    System.out.println("\n\tMin NO:"+zminmax[0]);
                                    System.out.println("\n\tMax NO:"+zminmax[1]);
                        }
}

OUTPUT:
Enter the no of elements
5
Enter the element at position:1
42
Enter the element at position:2
44
Enter the element at position:3
45
Enter the element at position:4
50
Enter the element at position:5
65
Min NO:42
Max NO:65




No comments:

Post a Comment