Created
May 29, 2018 03:11
-
-
Save thmain/aef100c5a8da98311db083f1a722c858 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BinarySearch { | |
private int [] arrA; | |
private int number; | |
public BinarySearch(int [] arrA){ | |
this.arrA = arrA; | |
} | |
public Boolean Search(int low,int high, int number){ | |
if(low>high){ | |
return false; | |
} | |
int mid = low + ((high - low) / 2); | |
if(arrA[mid]==number)return true; | |
else if (arrA[mid]>number) return Search(low,mid-1,number); | |
else return Search(mid+1,high,number); | |
} | |
public static void main(String args[]){ | |
int [] a = {2,5,8,10,14,44,77,78,99}; | |
int number = 99; | |
BinarySearch b = new BinarySearch(a); | |
System.out.println("The "+ number + " present in array a ??? :" + b.Search(0, a.length-1, number)); | |
number = 76; | |
System.out.println("The "+ number + " present in array a ??? :" + b.Search(0, a.length-1, number)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment