https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/

1. Use STL sort() and __builtin_popcount():

  • __builtin_popcount(unsigned int) is a builtin function in gcc to show how many bits in a unsigned int
    • 2 -> 10 __builtin_popcount(2) = 1
    • 3 -> 11 __builtin_popcount(3) = 2
    • 7 -> 111 __builtin_popcount(7) = 3
    • 8 -> 1000 __builtin_popcount(8) = 1
  • the 3rd parameter of sort() can use a non-default sorting rule ```cpp //12 ms 10.3 MB

class Solution { public: vector sortByBits(vector& arr) { sort(arr.begin(), arr.end(), { int pa = builtin_popcount(a); int pb = builtin_popcount(b); return pa==pb ? a<b : pa<pb; });
return arr; } };

  1. <a name="u3mjG"></a>
  2. #### 2. Use STL sort():
  3. - the 3rd parameter function of sort() should be stated static or out of a class
  4. ```cpp
  5. //36 ms 10.1 MB
  6. class Solution {
  7. public:
  8. vector<int> sortByBits(vector<int>& arr) {
  9. sort(arr.begin(), arr.end(), [](int a, int b){
  10. int pa = howManyBits(a);
  11. int pb = howManyBits(b);
  12. return pa==pb ? a<b : pa<pb;
  13. });
  14. return arr;
  15. }
  16. //return bits of a number
  17. static int howManyBits(int num){
  18. int count = 0;
  19. while(num > 0){
  20. if(num % 2 != 0){
  21. num = (num-1)/2;
  22. count++;
  23. } else{
  24. num = num/2;
  25. }
  26. }
  27. return count;
  28. }
  29. };

3. Use Merge Sort:

  1. //120 ms 26.6 MB
  2. class Solution {
  3. public:
  4. vector<int> sortByBits(vector<int>& arr) {
  5. vector<int> bit;
  6. int n = arr.size();
  7. //loop in arr
  8. for(int i=0; i<n; i++){
  9. //record number of bits
  10. bit.push_back(howManyBits(arr[i]));
  11. }
  12. mergeSortAdv(arr, bit);
  13. return arr;
  14. }
  15. private:
  16. //return bits of a number
  17. int howManyBits(int num){
  18. int count = 0;
  19. while(num > 0){
  20. if(num % 2 != 0){
  21. num = (num-1)/2;
  22. count++;
  23. } else{
  24. num = num/2;
  25. }
  26. }
  27. return count;
  28. }
  29. void mergeSortAdv(vector<int>& arr, vector<int>& bit){
  30. mergeSortAdv(arr, bit, 0, arr.size()-1);
  31. }
  32. void mergeSortAdv(vector<int>& arr, vector<int>& bit, int l, int r){
  33. if(l < r){
  34. int m = l + (r-l) / 2;
  35. mergeSortAdv(arr, bit, l, m);
  36. mergeSortAdv(arr, bit, m+1, r);
  37. merge(arr, bit, l, m, r);
  38. }
  39. }
  40. void merge(vector<int>& arr, vector<int>& bit, int l, int m, int r){
  41. vector<int> L_arr, R_arr;
  42. vector<int> L_bit, R_bit;
  43. int n1 = m - l + 1;
  44. int n2 = r - m;
  45. int i, j, k;
  46. for(i = 0; i < n1; i++){
  47. L_arr.push_back(arr[l + i]);
  48. L_bit.push_back(bit[l + i]);
  49. }
  50. for(j = 0; j < n2; j++){
  51. R_arr.push_back(arr[m + 1 + j]);
  52. R_bit.push_back(bit[m + 1 + j]);
  53. }
  54. i = 0;
  55. j = 0;
  56. k = l;
  57. while(i < n1 && j < n2){
  58. if(L_bit[i] <= R_bit[j]){
  59. if(L_bit[i] < R_bit[j]){
  60. bit[k] = L_bit[i];
  61. arr[k] = L_arr[i];
  62. i++;
  63. }
  64. else if(L_bit[i] == R_bit[j]){
  65. if(L_arr[i] <= R_arr[j]){
  66. bit[k] = L_bit[i];
  67. arr[k] = L_arr[i];
  68. i++;
  69. } else {
  70. bit[k] = R_bit[j];
  71. arr[k] = R_arr[j];
  72. j++;
  73. }
  74. }
  75. } else {
  76. bit[k] = R_bit[j];
  77. arr[k] = R_arr[j];
  78. j++;
  79. }
  80. k++;
  81. }
  82. while(i < n1){
  83. bit[k] = L_bit[i];
  84. arr[k] = L_arr[i];
  85. i++;
  86. k++;
  87. }
  88. while(j < n2){
  89. bit[k] = R_bit[j];
  90. arr[k] = R_arr[j];
  91. j++;
  92. k++;
  93. }
  94. }
  95. };

4. Use Swap Sort:

//72 ms    10.4 MB

class Solution {
public:
    vector<int> sortByBits(vector<int>& arr) {
        vector<int> bits;

        int n = arr.size();

        //loop in arr
        for(int i=0; i<n; i++){
            //record number of bits
            bits.push_back(howManyBits(arr[i]));
        }

        swapSortAdv(arr, bits);

        return arr;
    }

private:
    //return bits of a number
    int howManyBits(int num){
        int count = 0;
        while(num > 0){
            if(num % 2 != 0){
                num = (num-1)/2;
                count++;
            } else{
                num = num/2;
            }
        }

        return count;
    }

    //swap bits and corresponding arr positions
    void swapSortAdv(vector<int>& arr, vector<int>& bits){
        int n = arr.size();
        for(int i=0; i<n-1; i++){
            for(int j=i+1; j<n; j++){
                if(bits[i] > bits[j]){
                    swap(bits[i], bits[j]);
                    swap(arr[i], arr[j]);
                }
                if((arr[i] > arr[j]) && (bits[i] == bits[j])){
                    swap(arr[i], arr[j]);
                }
            }
        }
    }
};