-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
44 lines (37 loc) · 873 Bytes
/
Copy pathBinarySearch.java
File metadata and controls
44 lines (37 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class BinarySearch {
int[] input;
public BinarySearch(int[] input) {
// TODO Auto-generated constructor stub
this.input = input;
sortInput();
}
void sortInput()
{
InsertionSort is=new InsertionSort(input);
input=is.sort();
}
boolean search(int searchItem,int low, int high)
{
int middle=(low+high)/2;
if(low<=high) //this is the most important criteria for termination
{
if (searchItem==input[middle])
return true;
else if(searchItem>input[middle])
{
return search(searchItem,middle+1,high);
}
else
return search(searchItem,low,middle-1);
}
return false;
}
public static void main(String[] args)
{
int[] a={5,1,8,2,0,3};
BinarySearch bs=new BinarySearch(a);
for(int i=0;i<bs.input.length;i++)
System.out.println(bs.input[i]);
System.out.println(bs.search(8, 0, a.length));
}
}