Apologies in advance if this is very obvious or has been asked elsewhere already - I'm not very 'maths-y' so not sure how to ask this intelligently.
What is the best way to group a set of data values into n groups, such that each groups has the smallest possible range?
Lets say my sample values are as follows:
{1, 2, 7, 8, 9, 15, 16, 16, 17, 18, 18, 20}
and I am wanting to create 3 groups with the least amount of spread in each group. We could do three groups like so:
{1, 2, 7} spread = 6
{8, 9} spread = 1
{15, 16, 16, 17, 18, 18, 20} spread = 5
---------------------------------------------
total = 12
Or, if we rework the first two groups we can reduce it:
{1, 2} spread = 1
{7, 8, 9} spread = 2
{15, 16, 16, 17, 18, 18, 20} spread = 5
---------------------------------------------
total = 8
So my question is: is there a mathematical way of determining how the groups should be formed?
As a reverse version of the question, is there a way to specify not the number of groups, but the maximum spread a group can have, and figure out the groups such that they don't exceed the spread?
This may have some crossover to programming algorithms so I will post it on Stack Overflow as well.