in reply to Re^4: Modified Binary Search
in thread Modified Binary Search
Hm. You have a big list and a small list of dates, both sorted (or readily sortable).
And for each date in the small list, you want to find the subset of the big list that are 1 year either side.
And you search anew for each item in the small list?
Here's a one-pass algorithm:
my @bigSorted = ...; my @smallSorted = ...; my %ranges; my( $lo, $hi ) = ( 0, 0 ); for ( @smallSorted ) { my( $early, $late ) = calcBoundaryDates( $_ ); ++$lo while $bigList[ $lo ] lt $early; $hi //= $lo; ++$hi while $bigList[ $hi ] le $late; $ranges{ $_ } = $hi - $lo; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Modified Binary Search
by Limbic~Region (Chancellor) on Jan 15, 2010 at 14:19 UTC | |
|
Re^6: Modified Binary Search
by jdporter (Paladin) on Aug 11, 2011 at 16:02 UTC | |
by BrowserUk (Patriarch) on Aug 11, 2011 at 16:52 UTC | |
by Limbic~Region (Chancellor) on Aug 11, 2011 at 19:20 UTC |