Write a Python function to implement the K-Nearest Neighbors algorithm from scratch using only standard libraries.