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:
A Guide to Apache Commons Collections CollectionUtils
Creating a Generic Array in Java
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Xây dựng ứng dụng Client-Server với Socket trong Java
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Copy Constructor
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Spring Boot - Quick Start
Spring Cloud Connectors and Heroku
Spring @RequestParam Annotation
Java Program to add two large numbers using Linked List
Java Program to Find Number of Articulation points in a Graph
Spring Security OAuth Login with WebFlux
Java Program to Implement Shoelace Algorithm
HashMap trong Java hoạt động như thế nào?
A Guide to @RepeatedTest in Junit 5
Query Entities by Dates and Times with Spring Data JPA
Generating Random Numbers in a Range in Java
Guide to java.util.concurrent.Locks
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Implement Circular Singly Linked List
Java Program to Perform Cryptography Using Transposition Technique
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Spring Autowiring of Generic Types
Spring RestTemplate Request/Response Logging
Guide To CompletableFuture
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Implement Quick Sort with Given Complexity Constraint
Java Program to Check whether Undirected Graph is Connected using DFS
Java Program to Implement String Matching Using Vectors
Spring Data JPA Delete and Relationships