Rank algorithm