How to divide $n$ students to $m$ groups so that their level is as close as possible? I am currently teaching Maths to a large class of $200$ high school students. At the first class, I did a test that covered every lesson of Geometry (G), Algebra (A), Basic Statistics (S), and Trigonometry (T). I personally think 200 students in one class is too much so I want to divide them into $10$ classes with 20 students in each. My Question is, Given their results vector (G, A, S, T), how do I form the 10 groups such that the student's levels in each groups are as close to each other as possible?
 A: If you use the k-means algorithm to cluster your students, it will minimize the sum of squares within each group. This algorithm finds groups by penalizing skill variations in each group. The larger the differences in skill relative to the cluster mean, the larger will be the penalty. So, this should suit your needs.
You can do this very easily in R with the kmeans() command.
However, this algorithm does not enforce equal group sizes. Solutions to this limitation are discussed here.
A: The problem of finding the optimal class assignment is equivalent to minimizing the following objective function:
$$ \sum_{i=1}^{10} \sum_{j=1}^{20} \left\| \mathbf x_{ij} - \boldsymbol\mu_i \right\|^2 $$
where:
$$\mathbf x_{ij} = \text{vector of test scores of the } j^{th} \text{ student in the } i^{th} \text{ class}\\
\boldsymbol\mu_{i} = \text{mean of the test score vectors of the students in the } i^{th} \text{ class}$$
There are many ways to minimize such a function, but the one I generally prefer in small problems such as this is simulated annealing. Simulated annealing works by taking some initial proposed solution, and iteratively making tweaks to it (big tweaks at first, then progressively smaller ones) until eventually arriving at a final answer which is generally near-optimal.
In addition to an objective function, simulated annealing also requires a notion of what constitutes a "tweak" (more formally, what constitutes a pair of neighboring states). In this case such a tweak would correspond to swapping two random students from two random classes with one another.
Note: The objective function that I've provided is equivalent to summing over all within-class pairwise distances, as discussed in the Description section of the k-means wiki.
A: Do you think that would be an optimal assignment for the class? I would personally split the groups so that I have the top in each category in different groups. The students can leverage their skills and improve in that way.
I could frame the problem as 
$\arg\max\limits_{x \in Cat} Score_x \bigcup \arg\min\limits_{x \in Cat} Score_x$
where you are sampling without replacement.
