partialsort
is a standard library function and can be used like partialsort(re6, 1:5, rev=true)
to obtain 5 largest elements of the array.
partialsort
should beat MaxN4
for a bit larger value of N
like 15.
For a faster algorithm for a small N
you should iterate over the input array only once.