Given an array **arr[]** of positive integers, the task is to sort the array in decreasing order of count of set bits in binary representations of array elements.

For integers having same number of set bits in their binary representation, sort according to their position in the original array i.e., a stable sort. For example, if input array is {3, 5}, then output array should also be {3, 5}. Note that both 3 and 5 have same number set bits.

**Examples:**

Input:arr[] = {5, 2, 3, 9, 4, 6, 7, 15, 32}

Output:15 7 5 3 9 6 2 4 32

The integers in their binary representation are:

15 – 1111

7 – 0111

5 – 0101

3 – 0011

9 – 1001

6 – 0110

2 – 0010

4 – 0100

32 – 10000

Hence, the non-increasing sorted order is:

{15, 7, 5, 3, 9, 6, 2, 4, 32}

Input:arr[] = {1, 2, 3, 4, 5, 6};

Output:3 5 6 1 2 4

**Approach:** We have already discussed the method of sorting based on set bit count in the previous section with various methods. This post contains implementation using maps.

As we know that a map/multimap stores data in sorted manner. So if we store (32 – countsetbits(arr[i])) for an arr[i] in map, then the output will come out in decreasing order of set bit count which is the desired output.

Below is the implementation of the above approach:

## C/C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to sort the array according ` `// to the number of set bits in elements ` `void` `sortArr(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `multimap<` `int` `, ` `int` `> map; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `int` `count = 0; ` ` ` `int` `k = arr[i]; ` ` ` ` ` `// Counting no of setBits in arr[i] ` ` ` `while` `(k) { ` ` ` `k = k & k - 1; ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// The count is subtracted from 32 ` ` ` `// because the result needs ` ` ` `// to be in descending order ` ` ` `map.insert(make_pair(32 - count, arr[i])); ` ` ` `} ` ` ` ` ` `// Printing the numbers in descending ` ` ` `// order of set bit count ` ` ` `for` `(` `auto` `it = map.begin(); it != map.end(); it++) { ` ` ` `cout << (*it).second << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 5, 2, 3, 9, 4, 6, 7, 15, 32 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `sortArr(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# function to sort the array according ` `# to the number of set bits in elements ` `def` `sortArr(arr, n): ` ` ` ` ` `mp ` `=` `[] ` ` ` ` ` `for` `i ` `in` `range` `( n): ` ` ` `count ` `=` `0` ` ` `k ` `=` `arr[i] ` ` ` ` ` `# Counting no of setBits in arr[i] ` ` ` `while` `(k): ` ` ` `k ` `=` `k & k ` `-` `1` ` ` `count ` `+` `=` `1` ` ` ` ` `# The count is subtracted from 32 ` ` ` `# because the result needs ` ` ` `# to be in descending order ` ` ` `mp.append((` `32` `-` `count, arr[i])) ` ` ` ` ` `# Printing the numbers in descending ` ` ` `# order of set bit count ` ` ` `mp.sort(key ` `=` `lambda` `x: x[` `0` `]) ` ` ` `for` `it ` `in` `mp: ` ` ` `print` `(it[` `1` `], end` `=` `" "` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `5` `, ` `2` `, ` `3` `, ` `9` `, ` `4` `, ` `6` `, ` `7` `, ` `15` `, ` `32` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `sortArr(arr, n) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

**Output:**

15 7 5 3 9 6 2 4 32

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sort an array according to count of set bits
- Sort an array according to the increasing count of distinct Prime Factors
- Sort an array according to the order defined by another array
- Check if bits of a number has count of consecutive set bits in increasing order
- Sort an array of strings according to string lengths
- Sort an array according to absolute difference with a given value "using constant extra space"
- Python | Sort an array according to absolute difference
- Sort the array of strings according to alphabetical order defined by another string
- Sort an array according to absolute difference with given value using Functors
- Sort an array of strings according to string lengths using Map
- Sort an Array of Strings according to the number of Vowels in them
- Sort an array of Strings according frequency
- Sort integers in array according to their distance from the element K
- Sort the array according to their cubes of each element
- Sort an array according to absolute difference with given value
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Python | Sort a List according to the Length of the Elements
- Python | Sort a list according to the second element in sublist
- Sort the numbers according to their sum of digits
- Sort the numbers according to their product of digits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.