Binary search Program with Example:- A binary search locates an item in a sorted array by repeatedly dividing the search interval in half. Initially interval include the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

**Example****:-**

import java.util.Scanner; public class binarySearch { public int binSearch(int[] arr, int firIndex, int lasIndex,int search) //binSearch is an example of a method or a function { int mid = (firIndex + (lasIndex - firIndex) / 2); if(firIndex<lasIndex ) { if (search == arr[mid]) { return mid; } else if(search < arr[mid]) { if(search == arr[0]) return 0; return binSearch(arr, firIndex, mid, search); } else if(search > arr[mid]) { if(search == arr[mid+1]) return mid + 1; return binSearch(arr, mid+1, lasIndex, search); } } return -1; } public void sortarr(int[] arr) //this is also a class method { for(int x=0; x<arr.length; x++) { for(int y=x+1; y<arr.length; y++ ) { if(arr[x] > arr[y]) { int temp = arr[y]; arr[y]=arr[x]; arr[x]= temp; } } } for(int x=0; x<arr.length; x++) { System.out.print(arr[x] + " "); } } } public class Main //main class { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the size of the array: "); int a = sc.nextInt(); int[] x = new int[a]; System.out.print("Enter "+ a +" numbers: "); int mid; for(int i=0; i<n; i++) { x[i] = input.nextInt(); } binarySearch access = new binarySearch(); //this is instantiate an object to access a class System.out.println("The sorted numbers are: "); access.sort(x); //this access a method System.out.println(); System.out.print("Enter the number you want to search: "); int val = sc.nextInt(); System.out.print("The search number is on the index "); System.out.print(access.binSearch(x, 0, x.length-1, val)); //how to access a class } }

**Output:-**Enter the size of the array: 7

Enter 5 numbers: 3 5 34 21 20 10 9

The sorted numbers are:

3 5 9 10 20 21 34

Enter the number you want to search: 20

The search number is on the index 5

import java.util.*; class BinarySearch { public static void main (String[] args) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8 ,9}; //list from which integer found int found = binSearch(arr, 13, 0, arr.length - 1); if (found > -1) { System.out.println ("Integer found on location: " + found); } else { System.out.println("Integer not found"); } } static int binSearch(int[] arr, int k, int l, int h) { int mid; if (h < l) { return -1; } mid = (l + h) / 2; if (arr[mid] < k) return binSearch(arr, k, mid + 1, h); else if (arr[mid] > k) return binSearch(arr, k, l, mid - 1); else return mid; } }

**Output:-**Item found on location: 2

import java.util.*; public class BinarySearch { private static int arr[] = {2,5,7,8,9}; //list of integer in queue public static void main(String[] args) { BinarySearch b = new BinarySearch(); b.BinarySearch(arr); } public void BinarySearch(int[] args) { System.out.println("Binary search."); int upBound = args.length; int loBound = 1; int midpoint = (upBound + loBound) / 2; int diff = upBound - loBound; int search = 8; //Integer that search in queue by binary search for (int x = 0; x < args.length; x++) { if (search < args[midpoint - 1] && diff != 1) { upBound = midpoint - 1; midpoint = upBound / 2; } else if (search > args[midpoint - 1] && diff != 1) { loBound = midpoint + 1; midpoint = (loBound + upBound) / 2; } else if (search == args[midpoint - 1]) { midpoint = midpoint - 1; System.out.println("We found " + search + " at position " + midpoint + " in the list."); i = args.length; } else { System.out.println("We couldn't find " + search + " in the list."); i = args.length; } } } }

**Output:-**We found 8 at position 4 in the list.