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:

Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Guava – Join and Split Collections
The DAO with Spring and Hibernate
Java Program to Perform Cryptography Using Transposition Technique
Java Program to Create a Random Graph Using Random Edge Generation
Tạo chương trình Java đầu tiên sử dụng Eclipse
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java – Combine Multiple Collections
Phương thức forEach() trong java 8
Java Program to Implement Pollard Rho Algorithm
The XOR Operator in Java
Spring Boot - Building RESTful Web Services
Java – Write an InputStream to a File
HashMap trong Java hoạt động như thế nào?
ClassNotFoundException vs NoClassDefFoundError
Life Cycle of a Thread in Java
Java Program to Solve Knapsack Problem Using Dynamic Programming
Java Program to Implement Stack using Linked List
Java Program to Perform Quick Sort on Large Number of Elements
Java Program to Implement Flood Fill Algorithm
Java Program to Solve Tower of Hanoi Problem using Stacks
Spring Cloud – Adding Angular
LinkedList trong java
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Implement Caesar Cypher
Java – Create a File
Java Program to Perform Matrix Multiplication
Guide to java.util.Formatter
Java Program to Implement Ford–Fulkerson Algorithm
Java Program to Find Number of Articulation points in a Graph
Convert char to String in Java
Java Program to Implement Hash Tables Chaining with Binary Trees