希尔排序_希尔排序


希尔排序_希尔排序_02


希尔排序_i++_03


希尔排序_i++_04


希尔排序_希尔排序_05


希尔排序_希尔排序_06


希尔排序_希尔排序_07

class Program
{
static void Main(string[] args)
{
int[] sum = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
ShellSort(sum);
for (int i = 1; i < sum.Length; i++)
{
Console.Write(sum[i] + " ");
}

Console.ReadKey();
}

/// <summary>
/// 希尔排序
/// </summary>
/// <param name="array"></param>
static void ShellSort(int[] array)
{
int gap = array.Length / 2;

while (1 <= gap)
{
// 把距离为 gap 的元素编为一个组,扫描所有组
for (int i = gap; i < array.Length; i++)
{
int j = 0;
int temp = array[i];

// 对距离为 gap 的元素组进行排序
for (j = i - gap; j >= 0 && temp < array[j]; j = j - gap)
{
array[j + gap] = array[j];
}
array[j + gap] = temp;
}

Console.WriteLine("gap={0}", gap);
foreach (int n in array)
{
Console.Write("{0} ", n);
}
Console.WriteLine();

gap = gap / 2; // 减小增量
}
}
}