我需要一种在 c# 中使用 ShellSort 对数组进行排序的简单方法,请帮助我
问问题
1183 次
3 回答
10
使用外壳排序。
于 2010-12-14T14:52:27.477 回答
9
没有人会为您编写代码。你在那里学习。我会采取以下步骤:
找到算法的伪代码。阅读它,直到你理解它的作用。
将伪代码移植到 C#。
如果您在实施过程中遇到问题,请随时回来询问具体问题。
于 2010-12-14T14:54:51.593 回答
0
public static int[] shellSort(int[] ar)
{
//this gaps array works but is not unique
int[] gaps = new int[] { 1, 4, 10, 23, 57, 132, 301, 701 };
gaps = gaps.Reverse().ToArray();
Func<int, int, bool> potentialSwitch = (ind1, ind2) =>
{
if (ar[ind2] < ar[ind1])
{
int temp = ar[ind2];
ar[ind2] = ar[ind1];
ar[ind1] = temp;
return true;
}
return false;
};
foreach (int gap in gaps)
{
if (gap >= ar.Length)
continue;
for (int i = 0; i + gap < ar.Length; i++)
{
int j = i;
while (potentialSwitch(j, j + gap))
{
j -= gap;
if (j < 0)
break;
}
}
}
return ar;
}
于 2016-07-01T10:43:21.920 回答