You are given an array of length $n$. Each element of the array belongs to one of $K$ classes. You are supposed to rearrange the array using minimum number of swap operations so that all elements from the same class are always grouped together, that is they form a contiguous subarray.
For example:
$$
\begin{align*}
&[2, 1, 3, 3, 2, 2] \longrightarrow [2, 2, 2, 1, 3, 3], \text{ or} \\
&[2, 1, 3, 3, 2, 2] \longrightarrow [1, 2, 2, 2, 3, 3], \text{ or} \\
&[2, 1, 3, 3, 2, 2] \longrightarrow [3, 3, 2, 2, 2, 1].
\end{align*}
$$
Three other valid arrangements remain.
What is this problem called in literature? Is there an efficient algorithm for it?