human
Sort an array according to count of set bits | Set 2 | function to sort the array according to the number of set bits in elements.
Counting no of setBits in arr [ i ].
The count is subtracted from 32 because the result needs to be in descending order.
Printing the numbers in descending order of set bit count.
Driver code"
How can the above be solved in Python?
 Give me 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 sortArr ( arr , n ) :
	mp = [ ]
	for i in range ( n ) :
		count = 0
		k = arr [ i ]
		while ( k ) :
			k = k & k - 1
			count += 1
		mp . append ( ( 32 - count , arr [ i ] ) )
	mp . sort ( key = lambda x : x [ 0 ] )
	for it in mp :
		print ( it [ 1 ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
	arr = [ 5 , 2 , 3 , 9 , 4 , 6 , 7 , 15 , 32 ]
	n = len ( arr )
	sortArr ( arr , n )