I need to modify the below code to use binary search to return the Index of an Insertion Point in a sorted Array
for Instance if objArray={1,2,4} and searchObj=3
the binarysearch function should return 2 as the Index where 3 should be inserted
public int binarySearch(Comparable[] objArray, Comparable searchObj)
{
int low = 0;
int high = objArray.length - 1;
int mid = 0;
while (low <= high)
{
mid = (low + high) / 2;
if (objArray[mid].compareTo(searchObj) < 0)
{
low = mid + 1;
}
else if (objArray[mid].compareTo(searchObj) > 0)
{
high = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
Thanks