Rank of all elements in an array | Function to find rank ; Rank Vector ; Sweep through all elements in A for each element count the number of less than and equal elements separately in r and s . ; Use formula to obtain rank ; Driver Code
function rankify ( A , n ) { var R = [ ... Array ( n ) ] ; for ( var i = 0 ; i < n ; i ++ ) { var r = 1 , s = 1 ; for ( var j = 0 ; j < n ; j ++ ) { if ( j != i && A [ j ] < A [ i ] ) r += 1 ; if ( j != i && A [ j ] == A [ i ] ) s += 1 ; } R [ i ] = parseFloat ( r + parseFloat ( s - 1 ) / parseFloat ( 2 ) ) ; } for ( var i = 0 ; i < n ; i ++ ) document . write ( parseFloat ( R [ i ] ) . toFixed ( 1 ) + " " ) ; } var A = [ 1 , 2 , 5 , 2 , 1 , 25 , 2 ] ; var n = A . length ; for ( var i = 0 ; i < n ; i ++ ) document . write ( A [ i ] + " " ) ; document . write ( " " ) ; rankify ( A , n ) ;