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")

In reply to (tye)Re: joining and sorting arrays by tye
in thread joining and sorting arrays by hotshot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.