Monday, 27 November 2017

Java Program to Find Maximum Element in an Array using Binary Search


Code:

import java.util.Random;

public class Maximum_Using_Binary 
{
    static int N = 20;
    static int []sequence = new int[N];

    public static void sort()
    {
       int i, j, temp;
        for (i = 1; i< N; i++) 
        {
            j = i;
            temp = sequence[i];    
            while (j > 0 && temp < sequence[j-1])
            {
                sequence[j] = sequence[j-1];
                j = j-1;
            }
            sequence[j] = temp;            
        }        
    }

    public static void main(String args[])
    {
        Random random = new Random();

        for(int i=0; i
            sequence[i] = Math.abs(random.nextInt(100));
        System.out.println("The sequence is :");
        for(int i=0; i
            System.out.print(sequence[i] + " ");     

        sort();

        System.out.println("\nThe maximum element in the sequence is : " + sequence[N-1]);
    }
}


Output:

The sequence is :
40 60 99 69 71 90 33 83 7 79 49 67 24 23 36 46 55 13 98 8 
The miaximum element in the sequence is : 99



More Java Programs:

















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...