Optimizing the k-nearest neighbor algorithm