谢尔排序又称减小增量排序法,是对直接插入排序法的改进。具体实现如下:
代码
/// <summary>
/// 谢尔排序
/// </summary>
/// <param name="data"></param>
public static void ShellSort(int[] data)
{
if (data == null || data.Length < 1)
{
throw new ArgumentNullException("data");
}
int gap = data.Length / 2, temp;
while (gap >= 1)
{
for (int i = 0; i < data.Length - gap; i++)
{
if (data[i] > data[i + gap])
{
temp = data[i];
data[i] = data[i + gap];
data[i + gap] = temp;
}
}
gap = gap / 2;
}
}
/// 谢尔排序
/// </summary>
/// <param name="data"></param>
public static void ShellSort(int[] data)
{
if (data == null || data.Length < 1)
{
throw new ArgumentNullException("data");
}
int gap = data.Length / 2, temp;
while (gap >= 1)
{
for (int i = 0; i < data.Length - gap; i++)
{
if (data[i] > data[i + gap])
{
temp = data[i];
data[i] = data[i + gap];
data[i + gap] = temp;
}
}
gap = gap / 2;
}
}