以下是我对计数排序的尝试.我已经绘制了我的逻辑图,用口头表达,并彻底评论了我的代码.但是,我的代码会导致分段错误.我理解分段错误表示非法访问内存,因此这必然意味着我的一个索引值正试图访问数组范围之外的索引.但是,我无法弄清楚为什么会这样.
幸运的是,我的调试器突出显示了下面的行,我在评论中也注意到了这一点,其中发生了分段错误.尽管如此,我完全被难倒了.非常感谢任何帮助理解这个分段错误的性质,谢谢.
void sort(int values[], int n)
{
//create array of finite size (65536)
int countArray[INT_MAX];
//create array to eventually store sorted values
int sortedValues[n];
//loop through unsorted values to increment countArray index for each occurrence
for(int i = 0; i < n; i++) {
countArray[ values[i] ] += 1;
}
//starting index for sortedValues[]
int sortedIndex = 0;
//loop until we've reached the end of sortedValues[]
while(sortedIndex < n) {
//loop through each index value of countArray
//j represents …Run Code Online (Sandbox Code Playgroud)