The k-means clustering algorithm partitions n observations into k clusters where each observation belongs to the cluster with the nearest mean. It works by assigning every observation to a cluster whose mean yields the least within-cluster sum of squares, then recalculating the means to be the centroids of the new clusters. The algorithm iterates between these two steps until convergence is achieved. K-means clustering is commonly used for data mining and machine learning applications such as image segmentation.