develooper Front page | perl.fwp | Postings from March 2002

Re: Sort is lazy?!? (as in Haskell)

Thread Previous | Thread Next
From:
Jonathan E. Paton
Date:
March 10, 2002 14:16
Subject:
Re: Sort is lazy?!? (as in Haskell)
Message ID:
20020310221605.57743.qmail@web14602.mail.yahoo.com
> > PS  With a truly lazy sort you'd be able to do...
> > 
> > ($first) = sort { $a wibble $b } @big_list;  # in O(n)
> time.
> 
> Not necessarely - it would depend on the underlaying
> sorting mechanism.

It wouldn't be lazy then, would it?

> Shell sort and heap sort for instance don't get the
> first element first.

Huh?  Are you saything that those sorting algorithms
don't actually sort?

I think the idea of "Lazy sort" is in certain
circumstances, say for first X highest items, then you
can switch to a different and faster algorithm.

A lazy sort can be done in n time, easily:

@highest;
foreach element {
   if (element > lowest in @highest) {
       stick in @highest, removing lowest in @highest;
   }
}
return sort @highest

The Big Oh of that is n.

Jonathan Paton

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About