Coding the k-means algorithm