Is this an ok implementation of the binary search algorithm? It works but its an implementation I came up with and differs from my instructors. Can anyone punch a hole in it for me?
package algorithm.linearsearch;
public class BinarySearch {
public static void main(String[] args) {
System.out.println(binarySearch(
new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 26, 109, 1001, 1100 },
26));
}
private static int binarySearch(int[] array, int target) {
int p = 0;
int r = array.length - 1;
int q;
while (p <= r) {
q = (p + r) / 2;
if (array[q] == target) {
System.out.println("value: " + array[q]);
return q;
}
if (array[q] > target) {
r = q + 1;
} else {
p = q - 1;
}
}
return -1;
}
}