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.

  1.  
  2. package com.sanfoundry.combinatorial;
  3.  
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. public class BinarySearchSequence
  8. {
  9.     public static void searchSequence(int[] array, int[] search)
  10.     {
  11.         int first, last, middle;
  12.         first = 0;
  13.         last = array.length - 1;
  14.         boolean flag = true;
  15.         for (int i = 0; i < search.length; i++)
  16.         {
  17.             middle = (first + last) / 2;
  18.             while (first <= last && flag == true)
  19.             {
  20.                 if (array[middle] < search[i])
  21.                 {
  22.                     first = middle + 1;
  23.                 }
  24.                 else if (array[middle] == search[i])
  25.                 {
  26.                     System.out.println(search[i] + " found at location "
  27.                             + (middle + 1) + ".");
  28.                     first = 0;
  29.                     last = array.length - 1;
  30.                     break;
  31.                 }
  32.                 else
  33.                 {
  34.                     last = middle - 1;
  35.                 }
  36.                 middle = (first + last) / 2;
  37.             }
  38.             if (first > last)
  39.             {
  40.                 System.out
  41.                         .println(search[i] + " is not present in the list.");
  42.                 flag = false;
  43.             }
  44.         }
  45.     }
  46.  
  47.     public static void main(String args[])
  48.     {
  49.         int c, n, search[], array[];
  50.         Scanner in = new Scanner(System.in);
  51.         System.out.println("Enter number of elements: ");
  52.         n = in.nextInt();
  53.         array = new int[n];
  54.         Random rand = new Random();
  55.         for (c = 0; c < n; c++)
  56.         {
  57.             array[c] = rand.nextInt(100);
  58.         }
  59.         System.out.println("Elements: ");
  60.         for (int i = 0; i < array.length; i++)
  61.         {
  62.             System.out.print(array[i] + " ");
  63.         }
  64.         System.out.println("\nEnter length of sequence to find: ");
  65.         int m = in.nextInt();
  66.         search = new int[m];
  67.         System.out.println("Enter the sequence to find: ");
  68.         for (int i = 0; i < m; i++)
  69.         {
  70.             search[i] = in.nextInt();
  71.         }
  72.         searchSequence(array, search);
  73.         in.close();
  74.     }
  75. }

Output:

advertisement
$ 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.

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.