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

«
»

What is the Binary Search algorithm in java?

Binary search or binary search algorithm is a commonly used search algorithm that is designed to efficiently find the position of a target value within a sorted array or list. It works by dividing the search interval in half and comparing the value to the middle of the interval. This process is repeated until the value is found or determined to not exist.

Problem Description

Write a java program to search sequence using binary search algorithm to find an element.

Program/Source Code

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[c] = 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();
}
}```
Program Explanation

1. This Java program performs a binary search on an array of randomly generated integers to find a sequence of integers inputted by the user.
2. The user first inputs the number of elements to generate and the program creates an array of random integers of that size.
3. Then, the user inputs the length of the sequence they want to find and the program prompts the user to enter the sequence.
4. The searchSequence method uses a binary search algorithm to find the sequence within the array, and outputs the location of the sequence or indicates that it is not present in the array.
5. Finally, the main method calls searchSequence with the generated array and the user-inputted sequence to perform the search.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Testcases:

Test Case 1:

```\$ 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.```

Test Case 2:

Take Java Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
```\$ javac BinarySearchSequence.java
\$ java BinarySearchSequence
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.```

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”. 