Call ranking function only n times instead of 2*n*log(n).