I must create a program that searches for an int in arr1 by a binary search algorithm and return -1 if the int isn't found. I'm having trouble with creating subarrays. It works at the moment without them but I've only a workaround for whenever the int is greater than start.
public class Question8p2 {
public static void main(String[] args){
int[] arr1 = {2,45,234,567,876,900,976,999};
int start = arr1.length / 2;
int searchNum = 900;
arraySearch(start, searchNum, arr1);
System.out.println(arraySearch(start, searchNum, arr1));
}
public static int arraySearch(int start, int searchNum, int [] arr1){
if (arr1[start] == searchNum){
return start;
}
if (arr1[start] < searchNum){
start = start + 1;
}
if (arr1[start] > searchNum){
start = start / 2;
}
if (start == arr1[0] || start == arr1.length){
return -1;
}
return arraySearch(start, searchNum, arr1);
}
}
I'm having trouble with creating subarrays.startby one. You need start and end in your recursive method.