Algorithm:
The most common k-means algorithm repeatedly does these steps until convergence
Assignment step: Determine the centroid coordinates. Assign each observation to the centroid whose means yields the least within-cluster sum of squares.
Update step: Recalculate the new means to be the centroids of the observations assigned to the clusters.Assignment step: Determine the centroid coordinates. Assign each observation to the centroid whose means yields the least within-cluster sum of squares.
I used iris data set from UCI machine learning repository to implement k-means algorithm in R. Here is the code in R.
The cluster plot is shown below: