in reply to Re^3: Why does my get_max_index function return zero? (High Water Mark Algorithm)
in thread Why does my get_max_index function return zero? (High Water Mark Algorithm)
FWIW, here's a GRT (decorate-sort-undecorate) approach, but still O(n log n):
c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; use List::Util qw(shuffle); ;; use constant RA => shuffle -4 .. 4; use constant RA_MAX_I => $#{[ RA ]}; ;; for my $i_ins (0 .. RA_MAX_I+1) { my @rb = ((RA)[ 0 .. $i_ins-1 ], 99, (RA)[ $i_ins .. RA_MAX_I ]); my ($i_max) = map unpack('x[N] N', $_), reverse sort map pack('N N', 0x7fff_ffff+$rb[$_], $_), 0 .. $#rb ; ok $i_ins == $i_max, qq{largest at (@rb)[$i_ins]}; } ;; done_testing; " ok 1 - largest at (99 -1 -2 4 1 -3 2 3 0 -4)[0] ok 2 - largest at (-1 99 -2 4 1 -3 2 3 0 -4)[1] ok 3 - largest at (-1 -2 99 4 1 -3 2 3 0 -4)[2] ok 4 - largest at (-1 -2 4 99 1 -3 2 3 0 -4)[3] ok 5 - largest at (-1 -2 4 1 99 -3 2 3 0 -4)[4] ok 6 - largest at (-1 -2 4 1 -3 99 2 3 0 -4)[5] ok 7 - largest at (-1 -2 4 1 -3 2 99 3 0 -4)[6] ok 8 - largest at (-1 -2 4 1 -3 2 3 99 0 -4)[7] ok 9 - largest at (-1 -2 4 1 -3 2 3 0 99 -4)[8] ok 10 - largest at (-1 -2 4 1 -3 2 3 0 -4 99)[9] 1..10 ok 11 - no warnings 1..11
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Why does my get_max_index function return zero? (High Water Mark Algorithm)
by bliako (Abbot) on Jun 04, 2019 at 11:47 UTC |