k = min(i, K)
while k > 0 and tmp[1] < comp_list[k - 1][1]:
    comp_list[k] = comp_list[k - 1]
k -= 1
comp_list[k] = tmp

def search():
    for q in np_query:
        c = [(i, cv2.Mahalanobis(q, x, icovar)) for i, x in enumerate(
            np_gallery)]
insertionsort(c)
ids.append(map(lambda tup: tup[0], c[0:K]))
Nearest neighbors with uncertain points