Code:
void cntSort(unsigned m, unsigned n, unsigned data[])
{
unsigned temp[m]; // uninitialized VLA
unsigned *count = malloc(m * sizeof(*count)); // uninitiailzed dynamic array
for (int i = 0; i < n; i++) count[temp[i]]++;
for (int i = 0; i < m; i++) temp[count[data[i]]]++;
for (int i = 0; i < n; i++) data[i] = temp[i];
free(count);
}
Your code makes no sense. You're using the uninitialized values of temp to access the uninitialized values of count and increment them.
I think you want something more like this:
Code:
void cntSort(unsigned *data, unsigned size, unsigned maxValue) {
unsigned *count = calloc(maxValue + 1, sizeof *count);
for (unsigned i = 0; i < size; i++) count[data[i]]++;
for (unsigned val = 0, i = 0; val <= maxValue; val++)
for (unsigned j = 0; j < count[val]; j++)
data[i++] = val;
free(count);
}