Function to sort according to bit count . This function assumes that there are 32 bits in an integer .
static void sortBySetBitCount ( int [ ] arr , int n ) { List < int > [ ] count = new List < int > [ 32 ] ; for ( int i = 0 ; i < count . Length ; i ++ ) count [ i ] = new List < int > ( ) ; int setbitcount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { setbitcount = countBits ( arr [ i ] ) ; count [ setbitcount ] . Add ( arr [ i ] ) ; }