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ột số từ khóa trong Java
Java Program to Represent Graph Using Incidence List
Disable DNS caching
Chuyển đổi giữa các kiểu dữ liệu trong Java
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Stream Filter with Lambda Expression
Spring Boot - Internationalization
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Find the GCD and LCM of two Numbers
Spring RequestMapping
Using a List of Values in a JdbcTemplate IN Clause
How to Use if/else Logic in Java 8 Streams
Spring Boot - Hystrix
Integer Constant Pool trong Java
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to Implement Graph Coloring Algorithm
How to Manually Authenticate User with Spring Security
Custom Thread Pools In Java 8 Parallel Streams
Handle EML file with JavaMail
Java Program to Represent Graph Using 2D Arrays
Java Program to Check for balanced parenthesis by using Stacks
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Giới thiệu JDBC Connection Pool
Working with Network Interfaces in Java
Introduction to the Java ArrayDeque
Spring Boot Change Context Path
Spring Data JPA @Modifying Annotation
Java String Conversions
Create a Custom Auto-Configuration with Spring Boot
Java Program to Show the Duality Transformation of Line and Point
Spring RestTemplate Request/Response Logging