#!/usr/bin/env perl use strict; use warnings; use POSIX qw(ceil); sub lis(@) { # lis = Longest Increasing Subsequence: algorithm from https://en.wikipedia.org/wiki/Longest_increasing_subsequence, referenced # by BrowserUK at https://perlmonks.org/?node_id=1174579 my @X = @_; my (@P, @M); $#P = $#X; $#M = $#X + 1; my $L = 0; my $N = @X; for my $i ( 0 .. $N-1 ) { # binary search for j <= L # such that X[ M[j] ] < X[ i ] my $lo = 1; my $hi = $L; while( $lo <= $hi) { my $mid = ceil(($lo+$hi)/2.); if( $X[ $M[$mid] ] < $X[$i] ) { $lo = $mid + 1; } else { $hi = $mid - 1; } } # after searching, lo is 1 greater than the # length of the longest prefix of X[i] my $newL = $lo; # the predecessor of X[i] is the last index # of the subsequence of length newL-1 $P[$i] = $M[$newL -1 ]; $M[$newL] = $i; $L = $newL if $newL > $L; # if we found a subseq longer than any prev, update L } my @S; $#S = $L - 1; my $k = $M[$L]; for my $j (0 .. $L-1) { my $i = ($L-1)-$j; $S[$i] = $X[$k]; $k = $P[$k]; } return wantarray ? @S : [@S]; } use Algorithm::Combinatorics qw/combinations/; sub pryrt(@) { # my algorithm, from https://perlmonks.org/?node_id=1174328 my @array = @_; my $Fcount = @array; my @a; while( $Fcount ) { my $iter = combinations( \@array, $Fcount ); while( my $p = $iter->next() ) { my $monotonic = 1; @a = @$p; foreach my $i ( 0.. $#a-1 ) { my $j = $i + 1; $monotonic &&= $a[$j] > $a[$i]; last unless $monotonic; } return @a if $monotonic; } --$Fcount; } return wantarray ? @a : [@a]; } sub genarray { my $len = shift || rand 100; my $maxr = shift || rand 999; return map { int rand $maxr } 1 .. $len; } local $\ = "\n"; local $, = " "; my @array = genarray(20, 999); print scalar @array, "[@array]"; print "lis: [", lis(@array), "]"; print "pryrt: [", pryrt(@array), "]"; use Benchmark qw(cmpthese timethese); my $rv = timethese(-10, { lis => sub { lis @array; }, pryrt => sub { pryrt @array; }, }); cmpthese $rv; #### 20 [953 317 563 582 789 629 613 918 9 680 276 163 119 915 967 426 820 227 256 650] lis: [ 317 563 582 613 680 915 967 ] pryrt: [ 317 563 582 629 680 915 967 ] Benchmark: running lis, pryrt for at least 10 CPU seconds ... lis: 10 wallclock secs (10.51 usr + 0.00 sys = 10.51 CPU) @ 25210.27/s (n=265086) pryrt: 11 wallclock secs (11.26 usr + 0.00 sys = 11.26 CPU) @ 0.18/s (n=2) (warning: too few iterations for a reliable count) s/iter pryrt lis pryrt 5.63 -- -100% lis 3.97e-005 14197064% --