I wanna modify my counting sort algorithym to work with negative integers.
here is what i have so far(segmentation fault):
void counting_sort(int *vet, int max, int min, int n){
int i, j, C[(max-min)+1], B[n];
for (i=0;i<=max;i++){
C[i]=0;
}
for (i=0;i<n;i++){
C[vet[i]-min]++;
}
for (i=1;i<=(max-min);i++){
C[i]=C[i]+C[i-1];
}
for (i=n-1;i>=0;i--){
B[C[(vet[i])-min]-1]=vet[i];
C[vet[i]-min]--;
}
for (i=0;i<n;i++){
printf("%d ",B[i]);
}
}
I do not know what's causing the segmentation fault but if you want to sort negative numbers using counting sort, you just need to find the lowest negative number in the array and add the absolute value to all values of array temporary. After this, use the normal counting sort algo to sort the array then substract the lowest minimum value from every number in the array obtained.
Adding/Subtracting a number from all elements of the array do not affect the result of sorting.