-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchDemo.java
More file actions
31 lines (28 loc) · 864 Bytes
/
Copy pathBinarySearchDemo.java
File metadata and controls
31 lines (28 loc) · 864 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
public class BinarySearchDemo
{
public void binarySearch(int[] nums, int low, int high, int target)
{
int pos = 0;
pos = (low + high) / 2;
while((nums[pos] != target) && (low <= high))
{
if (nums[pos] > target)
{
high = pos - 1;
}
else
{
low = pos + 1;
}
pos = (low + high) / 2;
}
if (low <= high)
{
System.out.println("Found at index #" + pos);
}
else
{
System.out.println("not found.");
}
}
}