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 Represent Graph Using Incidence Matrix
Java Program to implement Bit Matrix
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Find Nearest Neighbor for Dynamic Data Set
Java Program to Find Nearest Neighbor Using Linear Search
Java Program to Implement Insertion Sort
Stack Memory and Heap Space in Java
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Functional Interfaces in Java 8
Java Program to Construct K-D Tree for 2 Dimensional Data
Hướng dẫn sử dụng Java Reflection
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement Word Wrap Problem
Convert String to int or Integer in Java
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
HttpClient 4 – Follow Redirects for POST
Mapping a Dynamic JSON Object with Jackson
Spring Boot - Apache Kafka
Custom Exception trong Java
Biến trong java
Java Program to Implement Sorted Vector
Iterating over Enum Values in Java
List Interface trong Java
Iterable to Stream in Java
Overflow and Underflow in Java
Injecting Prototype Beans into a Singleton Instance in Spring
Java Program to Implement Find all Cross Edges in a Graph
Using Custom Banners in Spring Boot
Spring 5 Testing with @EnabledIf Annotation
Hướng dẫn Java Design Pattern – Prototype