package Binarysearchprg;
public class Binaryrecursivesearch {
int mid;
public static int recursivesearch(int[] array,int first,int last,int search)
{
if(first<last)
{
int mid=(first+last)/2;
if(search<array[mid])
{
return recursivesearch(array, first, mid, search);
}
else{
if(search>array[mid])
return recursivesearch(array, mid+1, last, search);
else {
return mid;
}
}}
return (first+1);}
// -(first + 1);
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr1 = {2,45,234,567,876,900,976,999};
int index = recursivesearch(arr1,0,arr1.length,45);
System.out.println("Found 45 at "+index+" index");
index = recursivesearch(arr1,0,arr1.length,999);
System.out.println("Found 999 at "+index+" index");
index = recursivesearch(arr1,0,arr1.length,876);
System.out.println("Found 876 at "+index+" index");
}
}
public class Binaryrecursivesearch {
int mid;
public static int recursivesearch(int[] array,int first,int last,int search)
{
if(first<last)
{
int mid=(first+last)/2;
if(search<array[mid])
{
return recursivesearch(array, first, mid, search);
}
else{
if(search>array[mid])
return recursivesearch(array, mid+1, last, search);
else {
return mid;
}
}}
return (first+1);}
// -(first + 1);
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr1 = {2,45,234,567,876,900,976,999};
int index = recursivesearch(arr1,0,arr1.length,45);
System.out.println("Found 45 at "+index+" index");
index = recursivesearch(arr1,0,arr1.length,999);
System.out.println("Found 999 at "+index+" index");
index = recursivesearch(arr1,0,arr1.length,876);
System.out.println("Found 876 at "+index+" index");
}
}
No comments:
Post a Comment