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 Find Hamiltonian Cycle in an UnWeighted Graph
Annotation trong Java 8
Spring Cloud AWS – S3
A Quick Guide to Using Keycloak with Spring Boot
Guide to Java OutputStream
Auditing with JPA, Hibernate, and Spring Data JPA
Java Program to Implement Heap Sort Using Library Functions
Guide to Character Encoding
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Guide to the ConcurrentSkipListMap
Java Program to Check Cycle in a Graph using Topological Sort
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Upload and Display Excel Files with Spring MVC
Spring Boot: Customize Whitelabel Error Page
A Guide to @RepeatedTest in Junit 5
Java Program to Perform Searching in a 2-Dimension K-D Tree
Spring MVC Tutorial
Ép kiểu trong Java (Type casting)
Hướng dẫn Java Design Pattern – Decorator
Receive email using IMAP
Chuyển đổi từ HashMap sang ArrayList
Marker Interface trong Java
Mix plain text and HTML content in a mail
New Features in Java 11
The Modulo Operator in Java
Spring Data MongoDB – Indexes, Annotations and Converters
Java 8 StringJoiner
Java – Write to File
Spring MVC Content Negotiation