Evenly distributing items into buckets
<p>I'm working on a problem where I have 18 items that need to be sorted into 3 buckets.  Half of the items are red, and half are blue.  Also each item is of a specific size; however the sizes are not evenly distributed (I.e. 18, 20, 24, 18, 19, 26...).  I need an algorithm that can distribute these items into 3 buckets as follows:  1) Each bucket must end up with six items.  2) Each bucket must end up with 3 red items and 3 blue items. 3) (the part I'm struggling with) If you average the size of the six items in each bucket, they need to be as close as possible to the average size of the items in the other buckets.</p>

<p>I'm just learning to code, and am working on this problem as part of a project; however, I have been reading about sorting algorithms for a couple of days now, but I have not found any solutions that would help in solving the problem at hand, and I am thouroghly stumped.  I would prefer to come to the solution on my own, but would greatly appreciate a push in the right direction.</p>

<p>Thanks!</p>