ADVERTISEMENT
ADVERTISEMENT

Java program to search an item in an array using binary search

Given/input an integer array, we have to search an item in an array using binary search.
Submitted by Nidhi, on March 13, 2022

Problem Solution:

In this program, we will create an array of sorted integers then we will search an item into an array using binary search and print the position of the item in the array.

The binary searching algorithm is used to find the item in a sorted array with the minimum number of comparisons, in this algorithm key element compares with the middle index item.

Program/Source Code:

The source code to search an item into the array using binary search is given below. The given program is compiled and executed successfully.

// Java program to search an item in an array 
// using binary search

import java.util.Scanner;
public class Main {

  public static int BinarySearch(int arr[], int low, int high, int item) {
    if (high >= low) {
      int mid = low + (high - low) / 2;

      if (arr[mid] == item)
        return mid;

      if (arr[mid] > item)
        return BinarySearch(arr, low, mid - 1, item);

      return BinarySearch(arr, mid + 1, high, item);
    }
    return -1;
  }

  public static void main(String[] args) {
    Scanner SC = new Scanner(System.in);

    int i = 0;
    int n = 0;
    int arr[] = {10, 20, 30, 40, 50};

    int item = 0;
    int pos = 0;

    System.out.printf("Enter item to search: ");
    item = SC.nextInt();

    pos = BinarySearch(arr, 0, arr.length, item);

    if (pos == -1)
      System.out.printf("Item not found.");
    else
      System.out.printf("Item found at %d position.", pos);
  }
}

Output:

Enter item to search: 30
Item found at index 2.

Explanation:

In the above program, we imported the java.util.Scanner package to read the variable's value from the user. And, created a public class Main. It contains two static methods BinarySearch() and main().

The BinarySearch() method is used to search an item into the sorted array and return the index of the index to the calling method.

The main() method is an entry point for the program. Here, we created a sorted array. Then we read an item to be searched from the user using the Scanner class. Then we searched an item in an array using binary search and print index of a given item in the array.

Java Array Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.