Write a function to perform a k-nearest neighbor search on a high-dimensional vector space.
Implement knn_search(vectors, query, k), where vectors is a list of equal-length numeric vectors and query is one vector of the same dimension. Return the indices of the k nearest vectors, ordered by increasing squared Euclidean distance. Break equal-distance ties by smaller index. Use an approach that avoids sorting every vector when k is much smaller than the number of vectors.
The input contains at least k vectors, and all vector dimensions are positive.
def knn_search(vectors, query, k):