We have a select function as part of Base, which can do O(n) selection of
the top n:

julia> v = randn(10^7);

julia> let w = copy(v); @time sort!(w)[1:1000]; end;
elapsed time: 0.882989281 seconds (8168 bytes allocated)

julia> let w = copy(v); @time select!(w,1:1000); end;
elapsed time: 0.054981192 seconds (8192 bytes allocated)


So for large arrays, this is substantially faster.

On Mon, Dec 8, 2014 at 3:50 AM, Jeff Waller <[email protected]> wrote:

> This can be done in O(N).  Avoid sorting as it will be O(NlogN)
>
> Here's one of many Q on how
> http://stackoverflow.com/questions/7272534/finding-the-first-n-largest-elements-in-an-array
>

Reply via email to