Reputation: 944
Here's the stumper:
Start with three arrays A, B and C with a total of
2n+1
entries. Write an algorithm to sort all of the entries from all of the arrays using only the following two methods:
X = sort(X)
replaces the arrayX
with the sorted version.
(X , Y) = doubleUp(X , Y)
does nothing ifX
has more elements thanY
, otherwise it removes the firstlength(X)
entries fromY
and appends them to the end ofX
.
Here's what I've tried so far. If two of the arrays are empty, then just use sort
on the nonempty array.
If one of the arrays is empty, then I think I can use doubleUp
to get one array to have just one thing and the other array to have everything else, and if that singleton array has the smallest (or largest) element, then that works. So I can use sort
after I use doubleUp
each time to make sure this happens. I coded this up in Maple and it worked for all the cases I checked.
I have no idea how to do it with 3 arrays though. Anyone have any ideas?
Upvotes: 3
Views: 369
Reputation: 279285
Sounds like nonsense. The total number of entries is odd. The only way to increase the length of an array is to make it the smaller first argument of doubleUp
, in which case it ends up with an even number of elements. So unless all the elements are in one array to begin with there's no way to make one array contain all the elements, sorted or otherwise.
So, the desired final result is not a single array containing all the elements in order. Or if it is, then the answer to the question is "it cannot be done".
Upvotes: 5