human
Sort an array according to count of set bits | a utility function that returns total set bits count in an integer.
Function to sort according to bit count This function assumes that there are 32 bits in an integer ..
Traverse through all bit counts ( Note that we sort array in decreasing order ).
Utility function to pran array.
Driver Code"
Solution in Python:
 ... What is a code to sort an array according to count of set bits and the other steps required?
assistant
#Here is code to sort an array according to count of set bits and the other steps required.
 def countBits ( a ) :
	count = 0
	while ( a ) :
		if ( a & 1 ) :
			count += 1
		a = a >> 1
	return count
def sortBySetBitCount ( arr , n ) :
	count = [ [ ] for i in range ( 32 ) ]
	setbitcount = 0
	for i in range ( n ) :
		setbitcount = countBits ( arr [ i ] )
		count [ setbitcount ] . append ( arr [ i ] )
	for i in range ( 31 , - 1 , - 1 ) :
		v1 = count [ i ]
		for i in range ( len ( v1 ) ) :
			arr [ j ] = v1 [ i ]
			j += 1
def printArr ( arr , n ) :
	print ( * arr )
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len ( arr )
sortBySetBitCount ( arr , n )
printArr ( arr , n )