Given an integer sorted array (sorted in increasing order) and an element x, find the x in given array using binary search. Return the index of x. code example

Example: binary search iterative

// Binary Search using Iterative Approach

import java.io.*;
class Binary_Search
{
	public static void main(String[] args) throws Exception
	{
		Binary_Search obj = new Binary_Search();
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		System.out.println("Insert the length of the Array : ");
		int n = Integer.parseInt(br.readLine());
		int arr[] = new int[n];
		System.out.println("Insert elements into the array : ");
		for(int i=0;inum)
			{
				r = mid - 1;
			}
			else
			{
				l = mid + 1;
			}
		}
	}
}

Tags:

Misc Example