插入排序:该排序是一种简单的排序方法,基本操作就是将一个记录插入已经排好序的有序表中,从而得到一个新的,记录数增加一的有序表。
希尔排序:又称缩小增量排序(Diminishing Increment Sort),属于插入排序类的一种排序,在时间复杂度上有较大的改进。基本思想是先将整个待排序记录序列分成若干个子序列分别进行直接插入排序,当整个序列基本有序,再对全部记录进行一次直接插入排序。
1
namespace
Sorting
2![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
class SortingAlgorithms
4![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
private int[] arr;
6
private int upper;
7
private int numElement;
8![](/Images/OutliningIndicators/InBlock.gif)
9
//初始化数组
10
public SortingAlgorithms(int size)
11![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
arr = new int[size];
13
upper = size - 1;
14
numElement = 0;
15
}
16![](/Images/OutliningIndicators/InBlock.gif)
17
//给数组插入元素
18
public void Insert(int item)
19![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
arr[numElement] = item;
21
numElement++;
22
}
23![](/Images/OutliningIndicators/InBlock.gif)
24
//打印数组元素
25
public void DisplayElement()
26![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
for (int i = 0; i <= upper; i++)
28![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
Console.Write(arr[i] + " ");
30
}
31
Console.ReadLine();
32
}
33![](/Images/OutliningIndicators/InBlock.gif)
34
public void CleaArry()
35![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
for (int i = 0; i <= upper; i++)
37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38
arr[i] = 0;
39
numElement = 0;
40
}
41
}
42![](/Images/OutliningIndicators/InBlock.gif)
43
//选择排序,数据量大的时候效率较高
44
public void SelectionSort()
45![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
int Min, Temp;
47
for (int Outer = 0; Outer <= upper; Outer++)
48![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
Min = Outer;
50
for (int Inner = Outer + 1; Inner <= upper; Inner++)
51![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
if (arr[Inner] < arr[Min])
53![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
Min = Inner;
55![](/Images/OutliningIndicators/InBlock.gif)
56
}
57
Temp = arr[Outer];
58
arr[Outer] = arr[Min];
59
arr[Min] = Temp;
60![](/Images/OutliningIndicators/InBlock.gif)
61
}
62
}
63
}
64![](/Images/OutliningIndicators/InBlock.gif)
65![](/Images/OutliningIndicators/InBlock.gif)
66
//插入排序(版本一)
67
//插入排序当数据量较大的时候效率不高
68
public void InsertionSort()
69![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
int Inner, Temp;
71
for (int Outer = 1; Outer <= upper; Outer++)
72![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
Temp = arr[Outer];
74
Inner = Outer;
75
while (Inner > 0 && (arr[Inner - 1] >= Temp))
76![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
arr[Inner] = arr[Inner - 1];//向后移动二者较大的元素
78
Inner -= 1;
79![](/Images/OutliningIndicators/InBlock.gif)
80
}
81
arr[Inner] = Temp;
82
}
83
}
84![](/Images/OutliningIndicators/InBlock.gif)
85
//这个版本是和我们的C语言版的数据结构课本上的的类似
86
//插入排序(版本二)
87
public void InserSort()
88![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
int Inner, Temp;
90
for (int Outer = 1; Outer <= upper; Outer++)
91![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
//将每一趟比较的哨兵元素复制给Temp
93
Temp = arr[Outer];
94
for (Inner = Outer; (Inner > 0 && arr[Inner - 1] > Temp); Inner--)
95![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
//向后移动大的元素
97
arr[Inner] = arr[Inner - 1];
98
}
99
//将哨兵赋值
100
arr[Inner] = Temp;
101
}
102
}
103![](/Images/OutliningIndicators/InBlock.gif)
104![](/Images/OutliningIndicators/InBlock.gif)
105
//希尔排序(原书代码,有问题)
106![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*public void ShellSorts()
107
{
108
int Inner, Temp;
109
int h = 1;
110
while (h <= (numElement / 3))
111
{
112
h = h * 3 + 1;
113
while(h>0)
114
{
115
for (int Outer = h; Outer <= numElement - 1; h++)
116
{
117
Temp = arr[Outer];
118
Inner = Outer;
119![](/Images/OutliningIndicators/InBlock.gif)
120
while ((Inner > h-1 ) && arr[Inner - h] >= Temp)
121
{
122
arr[Inner] = arr[Inner - h];
123
Inner -= h;
124
}
125
arr[Inner] = Temp;
126
127
}
128
h = (h - 1) / 3;
129
}
130
}
131
132
}
133
*/
134![](/Images/OutliningIndicators/InBlock.gif)
135![](/Images/OutliningIndicators/InBlock.gif)
136
//希尔排序
137
public void ShellSortingAlgorithms()
138![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
139
int Temp;
140
//初始增量序列
141
int Increment = upper / 2;
142
while (Increment != 0)
143![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
144
for (int i = Increment; i <= upper; i++)
145![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
146
int j;
147
Temp = arr[i];
148
for (j = i; j >= Increment; j = j - Increment) //同子序列的插入排序
149![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
150
if (Temp < arr[j - Increment])
151
arr[j] = arr[j - Increment]; //如果后面的小于前面的,交换位置
152
else
153
break;
154
}
155
arr[j] = Temp; //插入
156
}
157
//减小增量序列
158
Increment /= 2;
159
}
160![](/Images/OutliningIndicators/InBlock.gif)
161
}
162![](/Images/OutliningIndicators/InBlock.gif)
163![](/Images/OutliningIndicators/InBlock.gif)
164
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
96
![](/Images/OutliningIndicators/InBlock.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
99
![](/Images/OutliningIndicators/InBlock.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
102
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
107
![](/Images/OutliningIndicators/InBlock.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/InBlock.gif)
119
![](/Images/OutliningIndicators/InBlock.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/InBlock.gif)
122
![](/Images/OutliningIndicators/InBlock.gif)
123
![](/Images/OutliningIndicators/InBlock.gif)
124
![](/Images/OutliningIndicators/InBlock.gif)
125
![](/Images/OutliningIndicators/InBlock.gif)
126
![](/Images/OutliningIndicators/InBlock.gif)
127
![](/Images/OutliningIndicators/InBlock.gif)
128
![](/Images/OutliningIndicators/InBlock.gif)
129
![](/Images/OutliningIndicators/InBlock.gif)
130
![](/Images/OutliningIndicators/InBlock.gif)
131
![](/Images/OutliningIndicators/InBlock.gif)
132
![](/Images/OutliningIndicators/InBlock.gif)
133
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
134
![](/Images/OutliningIndicators/InBlock.gif)
135
![](/Images/OutliningIndicators/InBlock.gif)
136
![](/Images/OutliningIndicators/InBlock.gif)
137
![](/Images/OutliningIndicators/InBlock.gif)
138
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
139
![](/Images/OutliningIndicators/InBlock.gif)
140
![](/Images/OutliningIndicators/InBlock.gif)
141
![](/Images/OutliningIndicators/InBlock.gif)
142
![](/Images/OutliningIndicators/InBlock.gif)
143
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
144
![](/Images/OutliningIndicators/InBlock.gif)
145
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
146
![](/Images/OutliningIndicators/InBlock.gif)
147
![](/Images/OutliningIndicators/InBlock.gif)
148
![](/Images/OutliningIndicators/InBlock.gif)
149
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
150
![](/Images/OutliningIndicators/InBlock.gif)
151
![](/Images/OutliningIndicators/InBlock.gif)
152
![](/Images/OutliningIndicators/InBlock.gif)
153
![](/Images/OutliningIndicators/InBlock.gif)
154
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
155
![](/Images/OutliningIndicators/InBlock.gif)
156
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
157
![](/Images/OutliningIndicators/InBlock.gif)
158
![](/Images/OutliningIndicators/InBlock.gif)
159
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
160
![](/Images/OutliningIndicators/InBlock.gif)
161
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
162
![](/Images/OutliningIndicators/InBlock.gif)
163
![](/Images/OutliningIndicators/InBlock.gif)
164
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
调用算法的过程:
1
static
void
Main(
string
[] args)
2![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
SortingAlgorithms MyArray = new SortingAlgorithms(10);
4
Random rnd = new Random(100);
5![](/Images/OutliningIndicators/InBlock.gif)
6
long Ticks = DateTime.Now.Ticks;
7
for (int i = 0; i < 10; i++)
8![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
MyArray.Insert((int)(rnd.NextDouble() * 100));
10
}
11![](/Images/OutliningIndicators/InBlock.gif)
12
Console.WriteLine("Before Sorting:");
13
MyArray.DisplayElement();
14
//选择排序
15
//MyArray.SelectionSort();
16![](/Images/OutliningIndicators/InBlock.gif)
17
//插入排序
18
//MyArray.InsertionSort();
19
//MyArray.InserSort();
20![](/Images/OutliningIndicators/InBlock.gif)
21
//希尔排序
22
MyArray.ShellSortingAlgorithms();
23
Console.WriteLine("After sorting");
24
//打印排序后的元素
25
MyArray.DisplayElement();
26![](/Images/OutliningIndicators/InBlock.gif)
27
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)