But I found some cases (very few, like 0.001%) where it does not work, ie. @array=(43,44,43); It would return (43,43) and (43,44).
Then it's not right and needs to be fixed!
I know about FAQ code, but it's very convenient if you can spend the extra computing time and memory, but not the programming time.
By all means go that route. You did say that you are limited to ~100 elements, so the following will not be a concern to you.
The problem is that the FAQ conflation means that in addition to storing the original arrays, you need to store duplicates of both their intersections and differences in addition to the counting hash. For small datasets it's not a problem, but once they start getting bigger it becomes a problem.
Each of these is a separate problem and should be deault with separately (in the FAQ or modules). Computing both (all three if you include union) in a single pass is an optimisation--but only if you need them all.
I also took a look at the local categorised answer for this, but that doesn't handle duplicates either. I'll get back to you when I've solved the problem.
I'm also on the track of an improvement to the algorithm. I noticed that it converges on a pretty good solution with very few iterations (often <100), but then no matter how many more iterations it never improves further.
However, on a different run with the same values, it will achieve a better solution, again arriving at it quickly and then never improving. and yet, sometime a thord run will find a further improvement which again is arrived at quickly. The key seems to be the state of the PRNG. Some starting points produce better results than others regardless of the number of iterations you apply.
This is where most of my effort has been going. I've been trying to analyse the algorithm to work out why once a non-optimal solution is arrived at (very quickly) even huge numbers of further iterations won't improve it. That required me to come up with a fast and repeatable shuffle implementation (and is the subject of another thread).
It's obviously similar to a "local minima" in a genetic algorithm, but I'm not yet seeing the pattern.
I'll get back to you when I have made progress on either problem.
In reply to Re^11: Divide array of integers into most similar value halves
by BrowserUk
in thread Divide array of integers into most similar value halves
by Pepe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |