```
public int searchInsert(int[] nums, int target) {
return binarySearch(nums, target, 0, nums.length - 1);
}
private int binarySearch(int[] nums, int target, int start, int end) {
if (start == end){
return target <= nums[start] ? start : start + 1;
}
if (start < end) {
int midpoint = (start + end) / 2;
if (target < nums[midpoint]) {
return binarySearch(nums, target, start, midpoint);
}
if (target > nums[midpoint]) {
return binarySearch(nums, target, midpoint + 1, end);
}
if (target == nums[midpoint]) {
return midpoint;
}
}
return 0;
}
```