-
Notifications
You must be signed in to change notification settings - Fork 0
/
1356 Sort Integers by the number of 1 bits.c
192 lines (162 loc) · 4 KB
/
1356 Sort Integers by the number of 1 bits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
void merge(int ** arr, int low, int mid, int high)
{
int * new_arr = (int *)malloc(sizeof(int) * ((high-low+1)*2));
int new_arr_index = 0;
int i=0,j=0;
for(i=low, j=mid+1; i<=mid && j<=high;)
{
if(arr[i][1] <= arr[j][1])
{
new_arr[new_arr_index++] = arr[i][0];
new_arr[new_arr_index++] = arr[i][1];
i++;
}
else
{
new_arr[new_arr_index++] = arr[j][0];
new_arr[new_arr_index++] = arr[j][1];
j++;
}
}
while(i<=mid)
{
new_arr[new_arr_index++] = arr[i][0];
new_arr[new_arr_index++] = arr[i][1];
i++;
}
while(j<=high)
{
new_arr[new_arr_index++] = arr[j][0];
new_arr[new_arr_index++] = arr[j][1];
j++;
}
j=low;
for(i=0; i<new_arr_index; )
{
arr[j][0] = new_arr[i++];
arr[j][1] = new_arr[i++];
j++;
}
free(new_arr);
}
void merge_1d(int * arr, int low, int mid, int high)
{
int * new_arr = (int *)malloc(sizeof(int) * (high-low+1));
int new_arr_index = 0;
int i=0,j=0;
for(i=low, j=mid+1; i<=mid && j<=high;)
{
if(arr[i] <= arr[j])
{
new_arr[new_arr_index++] = arr[i++];
}
else
{
new_arr[new_arr_index++] = arr[j++];
}
}
while(i<=mid)
{
new_arr[new_arr_index++] = arr[i++];
}
while(j<=high)
{
new_arr[new_arr_index++] = arr[j++];
}
memcpy(&arr[low], &new_arr[0], sizeof(int) * (high-low+1));
free(new_arr);
}
void merge_sort(int ** arr, int low, int high)
{
if(low < high)
{
int mid = (low+high)/2;
merge_sort(arr, low, mid);
merge_sort(arr, mid+1, high);
merge(arr, low, mid, high);
}
}
void merge_sort_1d(int * arr, int low, int high)
{
if(low<high)
{
int mid = (low+high)/2;
merge_sort_1d(arr, low, mid);
merge_sort_1d(arr, mid+1, high);
merge_1d(arr, low, mid ,high);
}
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* sortByBits(int* arr, int arrSize, int* returnSize)
{
int * ret_arr = (int *)malloc(sizeof(int) * arrSize);
int ret_arr_index = 0;
int ** matrix = (int **)malloc(sizeof(int *) * arrSize);
int matrix_index = 0;
int i=0;
for(i=0; i<arrSize; i++)
{
int * tmp_arr = (int *)malloc(sizeof(int) * 2);
int tmp_arr_index = 0;
matrix[i] = tmp_arr;
tmp_arr[0] = arr[i]; //Original value
//No of ones in the number
int num = arr[i];
int count = 0;
int bit_val = 0;
while(num > 0)
{
bit_val = (num & 0x01);
if(bit_val)
{
count++;
}
num = num>>1;
}
tmp_arr[1] = count; //No of ones
}
merge_sort(matrix, 0, arrSize-1);
for(i=0; i<arrSize; i++)
{
ret_arr[ret_arr_index++] = matrix[i][0];
}
int prev = INT_MIN;
int start_index = 0;
int end_index = 0;
int cur = 0;
for(i=0; i<arrSize; i++)
{
if(prev == INT_MIN)
{
prev = matrix[i][1];
cur = matrix[i][1];
}
else
{
cur = matrix[i][1];
if(prev != cur)
{
if(start_index != end_index)
{
merge_sort_1d(ret_arr, start_index, end_index);
start_index = end_index;
}
prev = matrix[i][1];
start_index++;
end_index++;
}
else if(prev == cur)
{
end_index++;
}
}
}
if(start_index != end_index)
{
merge_sort_1d(ret_arr, start_index, end_index);
}
*returnSize = ret_arr_index;;
return ret_arr;
}