Re: Algorithm Request
- Posted by Sabal.Mike at notations.com Apr 07, 2003
- 371 views
I did this manually, just to check myself. This is what I got: A = {377,378,391,424,425,488} B = {384,387,388,396,422,505} sumA = 2483 sumB = 2482 Method: There could easily be multiple answers to this problem, so I assume that any best answer will do. 1. Sort the list of numbers into ascending order 2. Take the bottom 25% and the top 25% and place them in column A. 3. Take the remaining center 50% and place them in column B. 4. SwapFactor = floor(abs(sumA - sumB)/2) 5. For each index x in A and index y in B, find the pair (x,y) so that abs(A[x]-B[y]) is closer to SwapFactor than any other pair. If sumA > sumB then A[x] > B[y] else A[x] < B[y]. 6. Swap A[x] with B[y] and recalc sumA and sumB remembering A[x] and B[y] in case we need to unswap. 7. SwapFactor2 = floor(abs(sumA-sumB)/2) 8. if SwapFactor2 > SwapFactor then unswap and stop, else repeat steps 4 through 8. I don't have time right now to code this and test it, and I'm sure someone else will find a much faster method in a few minutes anyway. HTH, Michael J. Sabal >>> cklester at yahoo.com 04/07/03 03:37PM >>> Is there an algorithm out there that will take a series of numbers and put them in groups of equal amounts? For instance, I have these numbers: 377 378 384 387 388 391 396 422 424 425 488 505 I need them placed in two groups, as close to being equal in value as possible. I'm sure there's something out there... if not, I challenge everyone to a EUPHORIA programming contest! :) TOPICA - Start your own email discussion group. FREE!