C#算法基础之希尔排序

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace ConsolePractice
  8 {
  9     class CArray
 10     {
 11         private int[] arr;
 12         //数组大小
 13         private int upper;
 14         //下标
 15         private int numElements;
 16 
 17         /// <summary>
 18         /// 初始化数组参数
 19         /// </summary>
 20         /// <param name="size"></param>
 21         public CArray(int size)
 22         {
 23             arr = new int[size];
 24             upper = size - 1;
 25             numElements = 0;
 26         }
 27 
 28         /// <summary>
 29         /// 插入方法
 30         /// </summary>
 31         /// <param name="item">存储的数</param>
 32         public void Insert(int item)
 33         {
 34             arr[numElements] = item;
 35             numElements++;
 36         }
 37 
 38         /// <summary>
 39         /// 输出方法
 40         /// </summary>
 41         public void DisplayElements()
 42         {
 43             for (int i = 0; i <= upper; i++)
 44             {
 45                 Console.Write(arr[i] + " ");
 46             }
 47             Console.WriteLine();
 48         }
 49 
 50         /// <summary>
 51         /// 清除数组
 52         /// </summary>
 53         public void Clear()
 54         {
 55             for (int i = 0; i <= upper; i++)
 56             {
 57                 arr[i] = 0;
 58             }
 59             numElements = 0;
 60         }
 61 
 62         /*
 63          * 希尔排序其实就是插入排序的改进,可以按任意数量自增
 64          */
 65         /// <summary>
 66         /// 希尔排序算法
 67         /// </summary>
 68         public void ShellSort()
 69         {
 70             int inner, temp;
 71             //每次按3自增
 72             int h = 3;
 73             while (h > 0)
 74             {
 75                 for (int outer = h; outer <= upper; outer++)
 76                 {
 77                     temp = arr[outer];
 78                     inner = outer;
 79                     //把一个元素取出来逐个跟之前h距离的元素比较。
 80                     //若该元素较小则较大的元素往右移。
 81                     //若该元素较大则结束循环。
 82                     while (inner > h-1 && arr[inner - h] >= temp)
 83                     {
 84                         arr[inner] = arr[inner - h];
 85                         inner -= h;
 86                     }
 87                     arr[inner] = temp;
 88                     this.DisplayElements();
 89                 }
 90                 //h--;
 91                 h = (h - 1) % h;
 92                 Console.WriteLine("h=" + h);
 93             }
 94         }
 95     }
 96 
 97     class C_shape
 98     {
 99         static void Main()
100         {
101             CArray nums = new CArray(10);
102             Random rnd = new Random(100);
103             for (int i = 0; i < 10; i++)
104             {
105                 nums.Insert(rnd.Next(0, 100));
106             }
107             Console.WriteLine("Before sorting:");
108             nums.DisplayElements();
109             Console.WriteLine("During sorting:");
110             nums.ShellSort();
111             Console.WriteLine("After sorting:");
112             nums.DisplayElements();
113             Console.ReadKey();
114         }
115     }
116 }

运行结果:

posted @ 2013-12-02 21:39  漂移青春  阅读(226)  评论(0编辑  收藏  举报