Implement K-means clustering with nearest-centroid assignments, centroid updates, convergence checks, and stable labels.