Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The warm-up problem isn't so trivial. Two possible solutions are decribed in Cormen – Leiserson – Rivest – Stein: Introduction to Algorithms. One of these is randomized and runs in expected O(n) time. (Update: the other one runs in guaranteed O(n) time, as Perl Mouse has noted in his reply. I'm sorry this wasn't clear from my original post.)

I've recently implemented this randomized algorithm for perl, although my implementation is not a very efficent one, as it would be possible to do all its operations in place (with only O(n) extra memory and more importantly less time).

The rest of this post shows my implementation.

Here's the code although it might not make much sense without context. The quantile function is passed a positive ordinal $r and a list of numbers @a. It returns the $r-th least number from the list if it would be sorted, with $r = 1 meaning to return the minimum of the list. If you want a median, pass $r = int((@a + 1)/2)

sub quantile { my($n, @a) = @_; my($q); @a < $n and return 9e9999; $q = quantile1($n, @a); if (1) { # DEBUG check postcondition my($nl, $nle); $nl = grep { $_ < $q } @a; $nle = grep { $_ <= $q } @a; $nl < $n && $n <= $nle or die "postcondition failed"; } $q; } sub quantile1 { my($n, @a) = @_; my($c, $v, @l, @h); @a <= 1 and do { @a or die "internal error: quantile1 called on empty + list, n=" . $n; return $a[0]; }; $c = @a[rand(@a)]; for $v (@a) { if ($v < $c) { push(@l, $v); } elsif ($c < $v) { push(@h, $v); } } if ($n <= @l) { quantile1($n, @l); } elsif ($n > @a - @h) { quantile1($n - @a + @h, @h); } else { $c; } }

In reply to Re: Puzzle: The Ham Cheese Sandwich cut. by ambrus
in thread Puzzle: The Ham Cheese Sandwich cut. by Perl Mouse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found