Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence

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:

Java Program to Solve any Linear Equation in One Variable
Introduction to Spring Boot CLI
Spring Boot - Building RESTful Web Services
Spring Boot - CORS Support
Toán tử trong java
An Intro to Spring Cloud Task
LinkedHashSet trong java
Java Program to Find the Connected Components of an UnDirected Graph
Java 8 – Powerful Comparison with Lambdas
HashSet trong java
Java Program to Create a Random Linear Extension for a DAG
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Get the workstation name or IP
Converting Java Date to OffsetDateTime
Java Program to Implement the Hill Cypher
Introduction to Java 8 Streams
Check if a String is a Palindrome in Java
Java Deep Learning Essentials - Yusuke Sugomori
Java Program to Decode a Message Encoded Using Playfair Cipher
How to Count Duplicate Elements in Arraylist
How to Get the Last Element of a Stream in Java?
Java Program to Implement HashMap API
Upload and Display Excel Files with Spring MVC
Generate Spring Boot REST Client with Swagger
How to Manually Authenticate User with Spring Security
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Guide to the Synchronized Keyword in Java
Jackson – JsonMappingException (No serializer found for class)
Jackson – Bidirectional Relationships
Configuring a DataSource Programmatically in Spring Boot
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Solve a Matching Problem for a Given Specific Case