laziness, impatience, and hubris PerlMonks

### Comment on

 Need Help??
bart,
Your code doesn't do the complete patience sorting, it does more or less sort, but you're left with some sorted piles of cards after it finishes — typically 11 piles for a deck of 52 cards, I read. You still need to do the second step: pick the lowest card from every pile, but you don't know what pile that is — except when you just start, then it's the leftmost one. It would be some form of merge sort.

No additional sorting is required to find the longest increasing subsequence. As far as picking the lowest card from every pile - that is easy, it is the top card in each pile.

```print "\$_->[-1][VAL]\n" for @pile;
I think you tripped over your wording. I believe you intended to say that at the end of the game, the cards are still not in complete order. While you can get the 1st card for free (the top card on the left most pile), the second card still requires scanning the lowest top card from each pile.

As I said, this is unnessary for finding the longest increasing subsequence which is what this meditation was about. The straight forward approach to finish the sorting would indeed be a merge sort. To make it even more efficient, the piles could be part of a balanced btree. You always select from the left-most pile and then re-insert that pile back into tree based off the card underneath.

The questions to ponder were from the perspective of still finding the longest increasing subsequence.

As far as the binary search. I have 2 versions here to get to the partial sort (sufficient to find the longest increasing subsequence) so benchmarking should be trivial. As far as to complete the sorting - I mentioned one possible way of making it more efficient then a merge sort above but I am not planning on taking it that far. IOW - left as an exercise for the reader. Here is the merge sort though so you have a complete answer to your original question in the CB:

```#!/usr/bin/perl
use strict;
use warnings;
use List::Util 'shuffle';

use constant VAL  => 0;
use constant PREV => 1;

my @list = shuffle(1..20);
print "@list\n";
print join " ", Long_Inc_Sub(@list);

sub Long_Inc_Sub {
my @list = @_;
my (@pile, @seq);

# Partial patience sort (w/o bin search) to get LIS

NUM:
for my \$num (@list) {
for (0 .. \$#pile) {
if (\$num < \$pile[\$_][-1][VAL]) {
my \$prev = \$_ ? \$#{\$pile[\$_ - 1]} : undef;
push @{\$pile[\$_]}, [\$num, \$prev];
next NUM;
}
}
my \$prev = @pile ? \$#{\$pile[-1]} : undef;
push @pile, [[\$num, \$prev]];
}

# Obtain LIS for return

my (\$prev, \$len) = (\$#{\$pile[-1]}, scalar @pile);
while (\$len--) {
\$seq[\$len] = \$pile[\$len][\$prev][VAL];
\$prev = \$pile[\$len][\$prev][PREV];
}

# Finish patience sorting via a mergesort
my @order;

while (@pile) {
my (\$low, \$idx) = (undef, undef);
for (0 .. \$#pile) {
my \$val = \$pile[\$_][-1][VAL];
(\$low, \$idx) = (\$val, \$_) if ! defined \$low || \$val < \$low
+;
}
push @order, \$low;
pop @{\$pile[\$idx]};
splice(@pile, \$idx, 1) if ! @{\$pile[\$idx]};
}

# print "\$_\n" for @order;
return @seq;
}
You can avoid push and splice by presizing @order and testing for definedeness respectively. These optimizations along with the use of the binary search are of course up to you.

Cheers - L~R

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?
 [stevieb]: perldigious: video near our hq in Montreal. Here in Calgary, it's -28c... we've only got 3" of snow so far, but there are several feet in the mountains already [perldigious]: Snowshoeing time stevieb? I need to buy a pair so I can walk my German Shepherd properly through the country. [stevieb]: yeah, I snowshoe a fair amount in the mountains. I also use spikes similar to these very often as well [stevieb]: LOL you can easily replace "Helsinki" with "Alberta" [perldigious]: I used to live at the top of a very steep hill stevieb, I can relate to the video. Literally used to have to build up momentum and try to force my vehicle to make it up the hill to park in my driveway properly... didn't always make it.

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2016-12-06 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
On a regular basis, I'm most likely to spy upon:

Results (112 votes). Check out past polls.