Rank of all elements in an array | Java Code to find rank of elements ; Function to print m Maximum elements ; 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
public class GfG { public static void rankify ( int A [ ] , int n ) { float R [ ] = new float [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int r = 1 , s = 1 ; for ( int 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 ] = r + ( float ) ( s - 1 ) / ( float ) 2 ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( R [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 2 , 5 , 2 , 1 , 25 , 2 } ; int n = A . length ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; System . out . println ( ) ; rankify ( A , n ) ; } }