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:
How to Remove the Last Character of a String?
Quick Guide to Spring Controllers
Java Program to Represent Graph Using Linked List
Deque và ArrayDeque trong Java
Java – Reader to String
Spring MVC Setup with Kotlin
Java Program to Implement Pollard Rho Algorithm
Partition a List in Java
Java Program to Implement Johnson’s Algorithm
Java 8 – Powerful Comparison with Lambdas
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Implement Bubble Sort
Java Program to Implement PriorityQueue API
A Comparison Between Spring and Spring Boot
Từ khóa this và super trong Java
Java Program to implement Associate Array
Java Program to Implement Patricia Trie
Java Program to Perform Partition of an Integer in All Possible Ways
HttpClient 4 Cookbook
String Joiner trong Java 8
Java Program to Solve a Matching Problem for a Given Specific Case
Spring Boot Annotations
String Processing with Apache Commons Lang 3
Jackson Annotation Examples
Java Program to Perform Searching Based on Locality of Reference
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Chuyển đổi Array sang ArrayList và ngược lại
Receive email using POP3
Java Program to Implement Vector API
Custom Thread Pools In Java 8 Parallel Streams
Java Program to Implement Circular Doubly Linked List
Converting a Stack Trace to a String in Java