Thursday, February 16, 2012

Sort an array according to count of set bits

Given an array of positive integers, 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.

http://www.geeksforgeeks.org/sort-array-according-count-set-bits/

No comments:

Post a Comment