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:
Mệnh đề Switch-case trong java
Java Program to Implement WeakHashMap API
Spring Security Login Page with React
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Hướng dẫn Java Design Pattern – Mediator
Java 14 Record Keyword
Check if a String is a Palindrome in Java
Java Program to Implement Affine Cipher
Vector trong Java
Java Program to Implement Aho-Corasick Algorithm for String Matching
Spring Boot - Tomcat Deployment
Posting with HttpClient
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Reading an HTTP Response Body as a String in Java
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Object Type Casting in Java
Artem and Array
HandlerAdapters in Spring MVC
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Implement Hopcroft Algorithm
Giới thiệu thư viện Apache Commons Chain
Java Program to Implement Borwein Algorithm
Using Spring ResponseEntity to Manipulate the HTTP Response
Apache Commons Collections OrderedMap
Logging a Reactive Sequence
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Database Migrations with Flyway
ClassNotFoundException vs NoClassDefFoundError
Java Program to Implement Gale Shapley Algorithm
Java Streams vs Vavr Streams
Dockerizing a Spring Boot Application
Guava CharMatcher