- #1

Arnoldjavs3

- 191

- 3

## Homework Statement

Java:

```
public class BinarySearch {
public static boolean search(int[] array, int searchedValue) {
int beginning = 0;
int end = array.length - 1;
while (beginning <= end) {
int middle = (beginning + end) / 2;
if (array[middle] == searchedValue) {
return true;
}
else if (array[middle] > searchedValue) {
end = middle - 1;
}
else if (array[middle] < searchedValue) {
beginning = middle + 1;
}
}
return false;
}
}
/*
middle = 3
array[3] > -3
end = 3;
middle = 0 + 3 /2 = 1.5 so 1
array[1]>-3
end = 1
middle = 0 + 1 /2 = 0
array[0] == -3
return true
*/
```

## Homework Equations

## The Attempt at a Solution

The following code is meant to search if an array has a searched value(from the user). My question is why do we need to do this:

end = middle - 1

and

beginning = middle + 1

I noticed that when I input some values without the + and - 1 in the algorithm that it still works, to a varying degree. Why is this?