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:

More Jackson Annotations
Multi Dimensional ArrayList in Java
Java Program to Perform Polygon Containment Test
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Java Program to Implement Hash Trie
Java Program to Implement Euler Circuit Problem
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Create a Custom Auto-Configuration with Spring Boot
Lập trình đa luồng với Callable và Future trong Java
Java Stream Filter with Lambda Expression
Java Program to Implement Min Hash
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement PrinterStateReasons API
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Write/Read cookies using HTTP and Read a file from the internet
String Processing with Apache Commons Lang 3
Jackson – Change Name of Field
Java Program to Implement Bloom Filter
Java Program to Check if it is a Sparse Matrix
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
REST Web service: Upload và Download file với Jersey 2.x
Programmatic Transaction Management in Spring
Lập trình hướng đối tượng (OOPs) trong java
Java Program to Generate Date Between Given Range
Tổng quan về ngôn ngữ lập trình java
Lớp TreeMap trong Java
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Hướng dẫn sử dụng Printing Service trong Java
Getting Started with Stream Processing with Spring Cloud Data Flow
Spring Cloud AWS – S3
Logout in an OAuth Secured Application
RestTemplate Post Request with JSON