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:
Spring Data JPA Delete and Relationships
Count Occurrences of a Char in a String
Hướng dẫn Java Design Pattern – State
Java InputStream to Byte Array and ByteBuffer
Java Program to Implement Find all Back Edges in a Graph
Java Program to Implement Segment Tree
CyclicBarrier in Java
Concrete Class in Java
Java Program to Implement Disjoint Sets
Java Program to Perform Insertion in a BST
Encode/Decode to/from Base64
Java Program to Construct K-D Tree for 2 Dimensional Data
Phân biệt JVM, JRE, JDK
Java – Combine Multiple Collections
Java Program to Implement EnumMap API
Properties with Spring and Spring Boot
Java Program to Generate Random Numbers Using Multiply with Carry Method
Spring Boot - Hystrix
Guide to Spring 5 WebFlux
Thao tác với tập tin và thư mục trong Java
Spring @RequestParam Annotation
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Implement Bresenham Line Algorithm
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Marker Interface trong Java
Working with Network Interfaces in Java
Reading an HTTP Response Body as a String in Java
Feign – Tạo ứng dụng Java RESTful Client
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Dockerizing a Spring Boot Application
Calling Stored Procedures from Spring Data JPA Repositories