Example Program to perform binary search on a list of integer numbers
This program uses binary search algorithm to search an element in given list of elements.
/* Program: Binary Search Example * Written by: Chaitanya from beginnersbook.com * Input: Number of elements, element's values, value to be searched * Output:Position of the number input by user among other numbers*/ import java.util.Scanner; class BinarySearchExample { public static void main(String args[]) { int counter, num, item, array[], first, last, middle; //To capture user input Scanner input = new Scanner(System.in); System.out.println("Enter number of elements:"); num = input.nextInt(); //Creating array to store the all the numbers array = new int[num]; System.out.println("Enter " + num + " integers"); //Loop to store each numbers in array for (counter = 0; counter < num; counter++) array[counter] = input.nextInt(); System.out.println("Enter the search value:"); item = input.nextInt(); first = 0; last = num - 1; middle = (first + last)/2; while( first <= last ) { if ( array[middle] < item ) first = middle + 1; else if ( array[middle] == item ) { System.out.println(item + " found at location " + (middle + 1) + "."); break; } else { last = middle - 1; } middle = (first + last)/2; } if ( first > last ) System.out.println(item + " is not found.\n"); } }
Output 1:
Enter number of elements: 7 Enter 7 integers 4 5 66 77 8 99 0 Enter the search value: 77 77 found at location 4.
Output 2:
Enter number of elements: 5 Enter 5 integers 12 3 77 890 23 Enter the search value: 99 99 is not found.
Why you are still using the old approach for binary search.
Directly we can use the utility methods of utility class – java.util.Arrays
Methods Example-
===============
public static int binarySearch(primitive() p,Primitive key)
public static int binarySearch(Object() o,Object key)
public static int binarySearch(Object() o,Object key,Comparator c)
Thanks Ashrumochan for your suggestion. It works fine and great share……
Nice one.. Thanks.
Thanks Ashrumochan Senapati,Good to know about “java.util.Arrays” class. Really helped a lot…
You made my day!!!
i found array index out of bound exception while executing this code.give me solution for that.
I just requesting a clarification , In my binary search algorithm classes it has been told that , if sort is not done before a Binary search it is almost equal to Linear search . In this example sorting of array is not done(Please correct me if I am missing something ) .
Yes, as far as i know me too think sorting is required. This program gives false output if u search for the value 8 considering the above numbers
Yes..Sorting is prerequisite for Binary Search.
Enter number of elements:
6
Enter 6 integers
12
3
5
7
9
4
Enter the search value:
4
4 is not found.
This is not binary search.. The basic requirement of binary search should be a presorted array in ascending order if iam not wrong…
Yes , we should sort before searching the element.
Arrays.sort(array)
Otherwise above program wont work