def searchInsert(nums, target):
    left, right = 0, len(nums) - 1
    
    while left <= right:
        mid = (left + right) // 2  # Find the middle index
        
        if nums[mid] == target:
            return mid  # Target found, return its index
        elif nums[mid] < target:
            left = mid + 1  # Search in the right half
        else:
            right = mid - 1  # Search in the left half
            
    return left  # Return the insertion position

# Example test cases
print(searchInsert([1, 3, 5, 6], 5))  # Output: 2
print(searchInsert([1, 3, 5, 6], 2))  # Output: 1
print(searchInsert([1, 3, 5, 6], 7))  # Output: 4
print(searchInsert([1, 3, 5, 6], 0))  # Output: 0
