Their purpose is to return the number of comparisons between the keys and the array items. Please let me know if there is anything that I should change, as I am new to Java and am not yet fully familiar with best practices.
public class BinaryVsLinear {
private static int linearSearch(int key, int[] array){
int count = 0;
for (int i = 0; i < array.length; i++){
count++;
if (array[i] == key){
i += array.length +1;
}
}
return count;
}
private static int binarySearch(int key, int[] array){
int count = 0, l = 0, r = array.length -1;
while (l <= r){
int m = (l+r)/2;
count++;
if (array[m] == key){
return count;
}
count++;
if (array[m] < key){
l = m + 1;
}
else{
r = m - 1;
}
}
return count;
}