希尔排序 C#
1
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
6 ShellSorter sh = new ShellSorter();
7 sh.Sort(iArrary);
8 for (int m = 0; m < iArrary.Length; m++)
9 Console.Write("{0} ", iArrary[m]);
10 Console.ReadLine();
11 }
12 }
4 {
5 int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
6 ShellSorter sh = new ShellSorter();
7 sh.Sort(iArrary);
8 for (int m = 0; m < iArrary.Length; m++)
9 Console.Write("{0} ", iArrary[m]);
10 Console.ReadLine();
11 }
12 }
2.
1 class ShellSorter
2 {
3 /// <summary>
4 /// 希尔排序
5 /// </summary>
6 public void Sort(int[] list)
7 {
8 int inc;
9 for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
10 for (; inc > 0; inc /= 3)
11 {
12 for (int i = inc + 1; i <= list.Length; i += inc)
13 {
14 int t = list[i - 1];
15 int j = i;
16 while ((j > inc) && (list[j - inc - 1] > t))
17 {
18 list[j - 1] = list[j - inc - 1];
19 j -= inc;
20 }
21 list[j - 1] = t;
22 }
23 }
24 }
2 {
3 /// <summary>
4 /// 希尔排序
5 /// </summary>
6 public void Sort(int[] list)
7 {
8 int inc;
9 for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
10 for (; inc > 0; inc /= 3)
11 {
12 for (int i = inc + 1; i <= list.Length; i += inc)
13 {
14 int t = list[i - 1];
15 int j = i;
16 while ((j > inc) && (list[j - inc - 1] > t))
17 {
18 list[j - 1] = list[j - inc - 1];
19 j -= inc;
20 }
21 list[j - 1] = t;
22 }
23 }
24 }
25 }
乌龟才背着房子过一辈子