Wednesday, September 28, 2016

Java- Smallest and Greatest Element of Array

This programs implements array and finds the smallest and greatest element of array. Do comment below. Have fun!!

class Arr
{
    Arr()
    {
    }
    int[] Ret(int a[])
    {
        for(int i=0;i<a.length;i++)
        {
            for(int j=0;j<a.length-1;j++)
            {
                if(a[j]>a[j+1])
                {   
                    int temp=a[j+1];
                    a[j+1]=a[j];
                    a[j]=temp;
                }
            }
        }
        return a;
    }
    public static void main(String ... args)
    {
        Arr a = new Arr();
        int b[]={9,1,3,0,2,4};
        int c[]=a.Ret(b);
        System.out.println("Smallest is"+c[0]);
        System.out.println("Largest is"+c[c.length - 1]);
    }
}
       
       

No comments:

Post a Comment