希尔排序_排序算法_算法

总感觉有点问题,是不是写错了

 #region 希尔排序
        public static void Main()
        {
            int[] array = { 6, 1, 4, 5, 0, 7, 2, 3, 9, 8, 23, 56, 89, 4, 56 };
            Sort(array);
            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine(array[i]);
            }
            Console.ReadKey();

        }
        public static void Sort(int []array)
        {
            int count = array.Length / 2;
            int temp=0;
            int n = 0;
            for (int i=count;i>=1 ;i-- )
            {
                for (int j = 0; j+i<array.Length; j++)
                {
                    if (array[j] > array[j + i])
                    {
                        temp = array[j];
                        array[j] = array[j + i];
                        array[j + i] = temp;
                    }
                    n++;
                }
               
            }
            Console.WriteLine("run:"+n);
        }
        #endregion
 
 
修改后的希尔排序:
 public static void Main()
        {
            //int[] array = { 6, 1, 4, 5, 0, 7, 2, 3, 9, 8, 23, 56, 89, 4, 56 };
            int temp = 0;
            int t = 0;
                 int num=0;
            for (int n = array.Length / 2; n > 0;n = n/2 )
            {

                for (int j = n; j <array.Length;j++ )
                {
                    t=j;
                    while (t - n >= 0)
                    {
                        if (array[t] < array[t - n])
                        {
                            temp = array[t-n];
                            array[t - n] = array[t];
                            array[t] = temp;
                        }
                        t = t - n;
                    }
                }
            }
            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine(array[i]);
            }
            Console.ReadKey();

        }

 

 

posted on 2013-05-30 00:09  staben  阅读(141)  评论(0编辑  收藏  举报

导航