Recursion program for binary search

import java.util.*;
class BinarySearchRecursion
{
static int A[]={3,6,8,15};
static int h=3,l=0,m=0;
public static void main(String args[])
{
System.out.println(“enter the number to search”);
Scanner sc= new Scanner(System.in);
int sk=sc.nextInt();
int flag=0;
/*   program for iterative binary search
 while(h>=l)
 {
  m=(h+l)/2;
 if(sk>A[m])
 l=m+1;
 else if(sk<A[m])
 h=m-1;
 else if(sk==A[m])
 {
 flag=1;
 break;
 }
 }
 if(flag==0)
 System.out.println(“not present”);
 else
 System.out.println(“present”);
 */
 flag=binarySearch(l,h,sk);
 if(flag!=-1)
 System.out.println(“element found at position”+flag);
 else
 System.out.println(“element not found”);

}
static int binarySearch(int l, int h, int element) {
   if(l>=h)
   return -1;
    m =(l+h)/2;
   if( A[m] == element )
   return m;
   else if(element < A[m] )
   return(binarySearch(l, m-1, element));
   else
   return(binarySearch(m+1, h, element));
}
}

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial