Hello everyone hoping you can help me here,
My problem is that I need to be able to search through an ArrayList using binary search and find where the correct place to add an object so that the list stays in order. I cannot use the collections sort as this is a homework. I have correctly implemented a boolean method that tells if the list contains an item you want to search for. That code is here:
public static boolean search(ArrayList a, int start, int end, int val){
int middle = (start + end)/2;
if(start == end){
if(a.get(middle) == val){
return true;
}
else{
return false;
}
}
else if(val < a.get(middle)){
return search(a, start, middle - 1, val);
}
else{
return search(a, middle + 1, end, val);
}
}
What I need to do is use this method to see if the number already exists in the list and if that returns false then I need to be able to use another binary search to figure out where in the list the number (val) should go. Any help is greatly appreciated, thank you!!!
Justin