【发布时间】:2013-11-14 21:14:37
【问题描述】:
所以我在程序中使用了快速排序,但现在想将复杂度降低到 O(n)。我需要使用桶排序才能工作。
我的程序会这样做
我的程序读入一个整数文件,以及文件中的整数个数,并输出文件中至少超过文件中数字 90% 的最小数字。
我确实设法让这个工作,使用快速排序。 但是,我没有使用桶排序得到正确的输出,我不知道为什么,因为我的代码似乎是正确的。
运行时出现分段错误
我的 Bucket 排序和输出结果代码
void Bucket_Sort(int array[], int n)
{
int i, j;
int count[n];
for(i=0; i < n; i++)
{
count[i] = 0;
}
for(i=0; i < n; i++)
{
(count[array[i]])++;
}
for(i=0,j=0; i < n; i++)
{
for(; count[i]>0;(count[i])--)
{
array[j++] = i;
}
}
} Bucket_Sort(array, numberOfNumbers);
//Output the lowest number in the file which exceeds at least 90% of the numbers in the file.
for (count = floor(0.9 * numberOfNumbers); count < numberOfNumbers; count ++)
{
if (array[count] != array[count + 1])
{
output = array[count];
break;
}
}
printf("The outputs is : ");
printf("%d \n", output);
我的程序输出编译,但运行时出现分段错误。
关于我在 BucketSort 中做错了什么有什么想法吗?
谢谢,
丹尼尔
【问题讨论】:
-
考虑更好地格式化您的代码,在当前状态下很难阅读。
-
对不起,我现在就改一下。我还在我的代码中发现了一个错误,所以我现在将对其进行更新。
标签: c arrays segmentation-fault asymptotic-complexity bucket-sort