Reputation: 25
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.
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.
Thanks!
Upvotes: 0
Views: 1260
Reputation: 8576
Here is a possible algorithm:
Let the red items in sorted order be r1
, r2
, r3
... r9
.
Let the blue items in sorted order be b1
, b2
, b3
... b9
.
Let the 3 buckets be B1
, B2
, B3
.
Put r1
and r9
in B1
, r2
and r8
in B2
, r3
and r7
in B3
.
Similarly, put b1
and b9
in B1
, b2
and b8
in B2
, b3
and b7
in B3
.
Now, we are left with r4
, r5
, r6
and b4
, b5
, b6
.
Put r4
and b6
in B1
, r5
and b5
in B2
, r6
and b4
in B3
.
Upvotes: 0