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

This is a java program to find the maximum element using binary search technique. Binary search requires sequence to be sorted. We return the last element of the sequence, which is maximum.

Here is the source code of the Java Program to Find Maximum Element in an Array using Binary Search. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a java program to find maximum element using Binary Search
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<N; i++)
            sequence[i] = Math.abs(random.nextInt(100));
        System.out.println("The sequence is :");
        for(int i=0; i<N; i++)
            System.out.print(sequence[i] + " ");     
 
        sort();
 
        System.out.println("\nThe maximum element in the sequence is : " + sequence[N-1]);
    }
}

Output:

$ javac Maximum_Using_Binary.java
$ java Maximum_Using_Binary
 
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

Related posts:

String Processing with Apache Commons Lang 3
Java Program to Implement LinkedList API
Java Program to Implement Regular Falsi Algorithm
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java – File to Reader
Java Program to Implement the Program Used in grep/egrep/fgrep
HttpClient 4 – Send Custom Cookie
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Find a Good Feedback Edge Set in a Graph
Java Program to Implement AA Tree
Java Program to Check Whether Graph is DAG
Java Collections Interview Questions
Java Program to Implement Extended Euclid Algorithm
Runnable vs. Callable in Java
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Implement Karatsuba Multiplication Algorithm
Java Program to Implement Heap Sort Using Library Functions
Java Program to Implement Expression Tree
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Quick Guide to java.lang.System
Java Program to Implement a Binary Search Tree using Linked Lists
Custom Thread Pools In Java 8 Parallel Streams
Lập trình hướng đối tượng (OOPs) trong java
DistinctBy in the Java Stream API
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Check Whether a Given Point is in a Given Polygon
@Order in Spring
Spring Boot - Service Components
A Guide to EnumMap
Lớp Arrarys trong Java (Arrays Utility Class)
Checking for Empty or Blank Strings in Java