Binary search program in java without recursion

 class SearchImp

{

private int arr[];

public SearchImp(int arr[])

{

this.arr=arr;

}

public static void main(String args[])

{

int[] arr={10,20,30,40,50};

SearchImp obj=new SearchImp(arr);

int high=arr.length-1;

int searchElement=30;

System.out.println("Search element "+searchElement+" is present "+obj.iterationSearch(0,high,searchElement));

}

public boolean iterationSearch(int low,int high,int searchElement)

{

boolean response=false;

while(low<=high)

{

int mid=(high+low)/2;

if(searchElement==arr[mid])

{

response=true;

return response;

}

else if(searchElement<arr[mid])

{

high=mid-1;

}

else

{

low=mid+1;

}

}

return response;

}

}



Post a Comment

0 Comments