我是 C 新手,正在尝试学习 shell 排序。我正在尝试按升序对整数数组进行排序。这是我的代码-
#include <stdio.h>
main()
{
int a[] = {1, 9, 7, 4, 8, 6, 7,2,1,6 };
int n =10; //array length
for (int c = (n / 2); c > 0; c = c / 2)
{
for (int i = c; c < n; i++)
{
int t = a[i];
int j;
for (j = i;( j >= c) && (t < a[j - c]); j = j - c)
{
a[j] = a[j - 1];
}
a[j] = t;
}
}
for (int i = 0; i <= 9; i++)
{
printf("%d ", a[i]);
}
}
在 Visual Studio Express 中编译此代码时,出现一条错误消息,要求关闭或调试,并且输出终端未显示任何输出。我无法弄清楚此代码中有什么问题。如果有人可以解释,我将不胜感激,也许会向我指出一个可以满足我要求的解决方案。
最佳答案
你的代码几乎是正确的,只需替换 for (int i = c; c < n; i++)
与 for (int i = c; i < n; i++)
'c' 总是小于 'n' 这就是你的代码陷入无限循环的原因。
关于c - C 中整数数组的 shell 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27754655/