I have implemented counting sort according to its pseudocode ( that's written on the blackboard in this video explanation ) but for some mysterious reason, it doesn't seem to work properly.
Every time I run this function, I get a segmentation fault as a result, although theoretically it should work.
My question is: I want to know why the code below will give Segmentation fault as an output.
void counting(int * array, int n){
int *copy, *out, i,j, max, counter;
out = (int*) malloc(sizeof(int) * n );
max = array[0];
// Find maximum value in main array and make a copy array of the same size
for(i=0;i<n;++i) if(array[i] > max) max = array[i];
copy = (int*) malloc(sizeof(int) * (max+1));
// initialize copy array
for(i=0;i<max;++i) copy[i] = 0;
// count how often each value occurs
for(i=0;i<n;++i) ++copy[array[i]];
// perform cumulative sum over the array
for(i=1;i<max;++i) copy[i] += copy[i-1];
// sort
for(i=n-1;i>=1;--i){
out[copy[array[i]]] = array[i];
--copy[array[i]];
}
// free memory
free(copy);
free(out);
// copies end result to original array
// for(i=0;i<n;++i) array[i] = out[i];
}