This is a java program to search sequence using binary search. This is a simple extension of binary search algorithm to find an element.
Here is the source code of the Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
package com.sanfoundry.combinatorial; import java.util.Random; import java.util.Scanner; public class BinarySearchSequence { public static void searchSequence(int[] array, int[] search) { int first, last, middle; first = 0; last = array.length - 1; boolean flag = true; for (int i = 0; i < search.length; i++) { middle = (first + last) / 2; while (first <= last && flag == true) { if (array[middle] < search[i]) { first = middle + 1; } else if (array[middle] == search[i]) { System.out.println(search[i] + " found at location " + (middle + 1) + "."); first = 0; last = array.length - 1; break; } else { last = middle - 1; } middle = (first + last) / 2; } if (first > last) { System.out .println(search[i] + " is not present in the list."); flag = false; } } } public static void main(String args[]) { int c, n, search[], array[]; Scanner in = new Scanner(System.in); System.out.println("Enter number of elements: "); n = in.nextInt(); array = new int[n]; Random rand = new Random(); for (c = 0; c < n; c++) { array = rand.nextInt(100); } System.out.println("Elements: "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println("\nEnter length of sequence to find: "); int m = in.nextInt(); search = new int[m]; System.out.println("Enter the sequence to find: "); for (int i = 0; i < m; i++) { search[i] = in.nextInt(); } searchSequence(array, search); in.close(); } }
Output:
$ javac BinarySearchSequence.java $ java BinarySearchSequence Enter number of elements: 10 Elements: 68 45 85 63 7 48 44 93 10 20 Enter length of sequence to find: 2 Enter the sequence to find: 7 48 7 found at location 5. 48 found at location 6. Enter number of elements: 10 Elements: 60 52 44 55 55 25 34 97 24 18 Enter length of sequence to find: 3 Enter the sequence to find: 2 3 4 2 is not present in the list. 3 is not present in the list. 4 is not present in the list.
Related posts:
Spring MVC Content Negotiation
How to Replace Many if Statements in Java
Converting String to Stream of chars
Enum trong java
Java Program to Check Cycle in a Graph using Topological Sort
Java Program to Evaluate an Expression using Stacks
Java Program to Implement Interpolation Search Algorithm
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Spring Boot - Database Handling
Jackson – Bidirectional Relationships
Spring Boot Tutorial – Bootstrap a Simple Application
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
A Quick Guide to Using Keycloak with Spring Boot
Java Program to Perform Insertion in a BST
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Implement Selection Sort
Lớp Collections trong Java (Collections Utility Class)
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Tính đa hình (Polymorphism) trong Java
Using the Map.Entry Java Class
The Spring @Controller and @RestController Annotations
Using Custom Banners in Spring Boot
Java Program to Find a Good Feedback Edge Set in a Graph
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java Program to Perform the Unique Factorization of a Given Number
How to Read HTTP Headers in Spring REST Controllers
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Java Program to Implement Triply Linked List
Java InputStream to String
Jackson JSON Views
XML-Based Injection in Spring
Spring Security Login Page with React