1. 程式人生 > >C#-控制檯-希爾排序法---ShinePans

C#-控制檯-希爾排序法---ShinePans

//希爾排序法
//縮小增量排序,先將整個帶排序的序列分割成若干的子序列,然後分別進行直接插入排序,帶整個序列黃總的數基本有序"時在對全體記錄進行一次直接插入排序.
using System;
class program
{
    #region
    public static void Sort(int[] arr)
    {
        int inc;
        for (inc = 1; inc <=arr.Length / 9; inc = 3 * inc + 1) ;
       
            for (; inc > 0; inc /= 3)
            {
                for (int i = inc + 1; i <= arr.Length; i += inc)
                {
                    int t = arr[i - 1];
                    int j = i;
                    while ((j > inc) && (arr[j - inc - 1] > t))
                    {
                        arr[j - 1] = arr[j - inc - 1];
                        j -= inc;
                    }
                    arr[j - 1] = t;
                }
            }
        
    }
    #endregion
    static void Main(string[] args)
    {
        int[] arr = new int[] { 21,4,26,18,32,54,47,9,15,48 };
        Console.Write("初始序列:");
        foreach (int n in arr)
            Console.Write("{0}", n + " ");
        Console.WriteLine();
        program.Sort(arr);
        Console.Write("排序後序列");
        foreach (int m in arr)
            Console.Write("{0}",m+" ");
        Console.ReadLine();
    }
}