简体   繁体   中英

Fair Sorting two arrays by values in another array

In ruby, I need to sort an array that contains ten hashes, in to two arrays.

Each hash has an entry named "points"

I need to sort the two arrays to be most fair on both parts.

thus if one hash has 1200 points, and another has 1220, and the rest 1000, I need to sort it out so the ones with 1200 and 1220 points doesnt get in the same array, if you follow :)

Hope the good folks of aardvark can help me!

This is called the partition problem . It is NP-complete, but according to Wikipedia "there are heuristics that solve the problem in many instances". They do describe or link to some of these approaches (unfortunately not in ready-to-use pseudocode). I guess if you only have 10 elements in your array, an exhaustive search of all 1024 possibilities is feasible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM