Implementing k nearest neighbor algorithms