Implementation of the k-means clustering algorithm.