in reply to joining and sorting arrays

You could do this, which would be O(N): Run through element in every array. For each one, you would do this:

  1. Increment a counter
  2. Add to a running total
  3. Keep track of the highest value you find.

Now, if the highest value you find isn't equal to your counter - 1 (subtract one because of the zero), you know you have a problem. And, if the total is not equal to the summation of 1 to your higest value, then you have a problem also. Otherwise, everything is correct.

Replies are listed 'Best First'.
(tye)Re: joining and sorting arrays
by tye (Sage) on Dec 04, 2001 at 23:41 UTC

    No. Fairly easy to find counter examples. Just start with a small working example:

    0 1 2 3 4 5
    then take two numbers near the middle that aren't 1 apart and add 1 to one of them and subtract 1 from the other:
    0 2 2 3 3 5

    Seems that a correct, fast soltuion would be a modified merge sort. I have a working example of such but I'm dubious of posting it as it now occurs to me that this is would be a fairly good, fairly advanced homework problem while I'm having a hard time coming up with a practical use for such.

    Perhaps you should tell us why you need this?

    Update: Just realized that the problem is even simpler than I first understood. I initially thought that the last example was a "good" example, not a "bad" example. I've updated my counter example to reflect that.

    This also means that a modified merge sort is overkill. Instead I'd just stuff the first element of each list into a hash and start at 0, look up the list that starts there, move down it verifying sequentialness, look up the list that starts at the next number, etc. I'm still not posting demonstration code, however, for most of the reasons I stated.

            - tye (but my friends call me "Tye")