-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1122.c
More file actions
45 lines (37 loc) · 853 Bytes
/
Copy path1122.c
File metadata and controls
45 lines (37 loc) · 853 Bytes
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
// Relative sort array
// EASY
#include <stdio.h>
#include <stdlib.h>
int* relativeSortArray(int* arr1, int arr1Size, int* arr2, int arr2Size, int* returnSize) {
int maxValue = 0;
for (int i = 0; i < arr1Size; i ++) {
maxValue = maxValue < arr1[i] ? arr1[i] : maxValue;
}
int* array = (int*)calloc(maxValue + 1, sizeof(int));
int* result = (int*)malloc((maxValue + 1) * sizeof(int));
int pos = 0;
for (int i = 0; i < arr1Size; i ++) {
array[arr1[i]] ++;
}
int j = 0;
while (j < arr2Size) {
if (array[arr2[j]] > 0) {
result[pos ++] = arr2[j];
array[arr2[j]] --;
} else {
j ++;
}
}
int i = 0;
while (i < maxValue + 1) {
if (array[i] > 0) {
array[i] --;
result[pos ++] = i;
} else {
i ++;
}
}
free(array);
*returnSize = arr1Size;
return result;
}