Daniel
Daniel

Reputation: 944

algorithm to sort elements of three arrays

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:

  1. X = sort(X) replaces the array X with the sorted version.

  2. (X , Y) = doubleUp(X , Y) does nothing if X has more elements than Y, otherwise it removes the first length(X) entries from Y and appends them to the end of X.

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

Answers (1)

Steve Jessop
Steve Jessop

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

Related Questions