我正在尝试基于基数排序的问题。 问题陈述:https://www.hackerearth.com/practice/algorithms/sorting/radix-sort/practice-problems/algorithm/monk-and-sorting-algorithm/#c186191
但是,我一直遇到SIGSEGV错误。我手动尝试跟踪代码,但找不到错误。
可能(可能)导致的事情:
#define range 100000
lli MAX(lli ar[], int size1){
int i; lli max1 = ar[0];
for(i = 0; i<size1; i++){
if(ar[i]>max1)
max1 = ar[i];
}
return max1;
}
void countsort(lli arr[],int n,lli place)
{
int i,freq[1000001]={0}; range from 0-9
lli output[n];
for(i=0;i<n;i++)
freq[(arr[i]/place)%range]++;
for(i=1;i<1000001;i++)
freq[i]+=freq[i-1];
for(i=n-1;i>=0;i--)
{
output[freq[(arr[i]/place)%range]-1]=arr[i];
freq[(arr[i]/place)%range]--;
}
for(i=0;i<n;i++)
arr[i]=output[i];
for(i = 0; i<n; i++)
printf("%lld ", arr[i]);
printf("\n");
}
void radixsort(lli arr[],int n,int maxx) //maxx is the maximum element in the array
{
lli mul=1;
while(maxx)
{
countsort(arr,n,mul);
mul*= range;
maxx/= range;
}
}
int main(){
int size, i; long long int ar[1000001];
scanf("%d", &size);
for(i = 0; i<size; i++)
scanf("%lld", &ar[i]);
lli max = MAX(ar, size);
radixsort(ar, size, max);
return 0;
}
我得到的错误消息:
Execution failed.
Segmentation Fault : This occurs because of an out-of-scope array index that is causing a buffer overflow, an incorrectly initialized pointer, etc. A signal is generated when a program either tries to read or write outside the memory that is allocated for it or to write memory that can only be read. For example, you are accessing a[-1] in a language that does not support negative indices for an array.