def search_bisection(array, val):
    """Finds the index of the nearest value to val in array. Uses the bisection method.

    Parameters
    ----------
    array : list of numeric values
        list to search, assumes the list is sorted (will not work if it isn't sorted!)
    val : numeric
        numeric value to find the nearest value in the array.

    Returns
    -------
    int
        returns the index of the position in the array with the value closest to val

    Examples
    --------
    
    >>> list_to_search = [1,2,3,4,5]
    >>> val_to_find    = 2.5
    >>> idx = search_bisection(list_to_search, val_to_find)
    >>> idx, list_to_search[idx]
    (1, 2)
    
    """
    jlower = 0
    jupper = len(array) - 1
    if val <= array[jlower]:
        return jlower
    elif val >= array[jupper]:
        return jupper
    while jupper - jlower > 1:
        jmid = (jupper + jlower) // 2
        if val >= array[jmid]:
            jlower = jmid
        else:
            jupper = jmid
    return jlower
