basically you are trying to create a space the size of max value and fill it with values(count of the array values in their position in v3) >=0, then refill our array with the values from the space v3
the only change I'd suggest is to completely remove the v2 array
void sort(int *v, int d) {
int max = 0;
for (int i = 0; i < d; i++) {
if (v[i] > max)max = v[i];
}
int v3[max + 1];
for (int i = 0; i < max + 1; i++) {
v3[i] = 0;
}
for (int i = 0; i < d; i++) {
if (v3[v[i]] == 0) {
v3[v[i]]++;
} else {
v3[v[i]]++;
}
}
int j = 0;
for (int i = 0; i < max + 1; i++) {
if (v3[i] != 0) {
for (int k = 0; k < v3[i]; k++) {
v[j] = i;
j++;
}
}
}
}
Note:
- this sort mechanism takes up too much memory in case of numbers with greatly varying differences
- will not support negative values