Problems? Is your data what you think it is? PerlMonks

### Comment on

 Need Help??
use strict; use warnings; my (@arr, @sequence, @result, \$p, \$node); @arr = 1..20; randomize(\@arr); print "@arr\n"; for (@arr) { \$p = simplefind(\@sequence, \$_); \$node = {'key' => \$_}; if (!\$p) { \$sequence[1] = \$node; } else { \$node->{'last'} = \$sequence[\$p]; \$sequence[\$p+1] = \$node; } } \$node = \$sequence[-1]; while (\$node) { push @result, \$node->{'key'}; \$node = \$node->{'last'}; } print join ' ', reverse @result; sub simplefind { my (\$p, \$key, \$i) = @_; for (\$i = 1; \$i <= \$#\$p; \$i++) { last if \$key < \$p->[\$i]{'key'}; } return \$i-1; } sub randomize { my (\$p, \$size, \$swap, \$key); \$p = \$_[0]; \$size = \$#\$p; for (0..(\$size-1)) { \$swap = int rand(\$size - \$_ + 1) + \$_; \$key = \$p->[\$_]; \$p->[\$_] = \$p->[\$swap]; \$p->[\$swap] = \$key; } }
You build an array of sequences..

1-sequence
2-sequence
3-sequence
etc.

Where each n-sequence is the n-sequence with the smallest possible end item. By the nature of the structure, the end items will always be listed in increasing order - the end item for the 2-sequence will always be less than or equal to the end item for the 3-sequence.

Now, for each new item, just find the sequence with the largest end item that is smaller than or equal to the current item. Link your current item to this sequence and link the slot for the next largest sequence to your current item. Rinse and repeat until all items are processed.

For instance, given 3, 5, 2, 1, 7:

3: (3)
5: (3, 3-5))
2: (2, 3-5))
1: (1, 3-5))
7: (1, 3-5, 3-5-7)

The only really hard part is coming up with a binary find for the largest item that's smaller than or equal to the current item. I know it's possible, but the exact algorithm eludes me. I'm currently using simplefind() for test purposes - an O(n) algorithm rather than the proper O(lg n). Fixing this will make the entire algorithm O(n lg n). (EDIT: Actually, it's O(n lg s), where s is the longest sequence, but worst-case is s = n)

(You may also notice, by the way, that the entire sequence is reconstructed from data stored along the way. It's also possible to store just the the subscript of the end item in each n-length sequence, then do a linear find backwards from that point, recreating the sequence as you go from all the items smaller than or equal to the current part of the sequence.)

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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [jedikaiti]: Good \$localtime, Monks

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2017-12-12 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What programming language do you hate the most?

Results (335 votes). Check out past polls.

Notices?