def binary_search(dep_times_list, start_time):
    """
    Function representing the binary search algorithm, from a list of ordered
    values (departure times) chooses value equal or closest (higher) to given
    parameter
    Parameters
    ----------
    dep_times_list: list of ordered datetime objects
    start_time: datetime object

    Returns: tuple of selected departure time and index of the time
    -------

    """
    #if start time earlier than all departure times, the first departure time
    #is chosen
    if (start_time <= dep_times_list[0]):
        return (dep_times_list[0], 0)
    #if start time is after all departure times, no connection is available
    #within this route, the best time is set to None
    if start_time > dep_times_list[len(dep_times_list) - 1]:
        return None,None
    left_index = 0
    right_index = len(dep_times_list) - 1

    while left_index <=right_index:
        if left_index == right_index:
            return (dep_times_list[left_index], left_index)
        middle_index = (left_index + right_index) // 2
        middle_time = dep_times_list[middle_index]
        if start_time > middle_time:
           left_index = middle_index + 1
        elif start_time < middle_time:
           right_index = middle_index
        else:
            return (dep_times_list[middle_index], middle_index)
    return (None, None)