C#排序(二):希尔排序
class Program
{
static void Main(string[] args)
{
int[] array = new int[] {41,12,52,69,89,78,54,62,53,10};
Console.WriteLine("希尔排序前:");
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i]+".");
}
ShellSort(array);
Console.WriteLine();
Console.WriteLine("希尔排序后:");
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i]+".");
}
Console.WriteLine();
}
private static void ShellSort(int[] array)
{
int temp;
int n = array.Length;
//初始步长
int gap = n / 2;
while (gap!=0)
{
for (int i = gap; i < array.Length; i++)
{
int j;
temp=array[i];
//同子序列的插入排序
for (j= i; j>=gap; j=j-gap)
{
if (temp < array[j - gap])
{
//如果后面的小于前面的,交换位置
array[j] = array[j - gap];
}
else
break;
}
//插入
array[j] = temp;
}
//缩短步长
gap /= 2;
}
}
}