```
public int searchInsert(int[] nums, int target) {
int lo = 0;
int hi = nums.length-1;
while (lo <= hi) {
int mid = (lo + hi) >>> 1;
int val = nums[mid];
if (val == target) {
return mid;
}
else if (target < val) {
hi = mid-1;
}
else {
lo = mid+1;
}
}
return lo;
}
```