I would do four changes:
- Better variable names, especially for the arguments
- Remove unnecessary temporary array
- Handle negative numbers
- Initialize temporary array with
memset
instead of a loop. - Comments with pre and post conditions
The code looks like this:
/* Preconditions:
array is a pointer to the array that should be sorted
length is the number of elements in the array
Postconditions:
array is sorted
*/
void sort(int *array, int length) {
if(!array) return;
int max = list[0];
int min = max;
for (int i = 0; i < length; i++) {
if(max < list[i]) max = array[i];
if(min > array[i]) min = array[i];
}
const int range = max - min + 1;
int tmp[range];
memset(tmp, 0, sizeof tmp);
for (int i = 0; i < length; i++) tmp[array[i] - min]++;
int index = 0;
for (int i = 0; i < range; i++) {
for (int j = 0; j < tmp[i]; j++) {
array[index] = i + min;
index++;
}
}
}