实现了几种比较常见的排序方法,比较简单。
1
using
System;
2
using
System.Collections;
3
using
System.ComponentModel;
4
using
System.Data;
5
using
System.Drawing;
6
using
System.Web;
7
using
System.Web.SessionState;
8
using
System.Web.UI;
9
using
System.Web.UI.WebControls;
10
using
System.Web.UI.HtmlControls;
11
12
namespace
suanfa
13
{
14
/**//// <summary>
15
/// Sort 的摘要说明。
16
/// </summary>
17
public class Sort : System.Web.UI.Page
18
{
19
private void Page_Load(object sender, System.EventArgs e)
20
{
21
int i;
22
int [] arrSortData;
23
int[] arrData = new int[]
{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
24
//输出未排序数据
25
Response.Write("<b>原来数据:<b><br>");
26
for(i=0; i<arrData.Length; i++)
27
{
28
Response.Write(arrData[i] + " ");
29
}
30
Response.Write("<hr>");
31
//输出冒泡排序结果
32
arrSortData = bubbleSort(arrData);
33
Response.Write("<b>冒泡排序结果:<b><br>");
34
for(i=0; i<arrSortData.Length; i++)
35
{
36
Response.Write(arrSortData[i] + " ");
37
}
38
Response.Write("<hr>");
39
//输出快速排序结果
40
arrSortData = quickSort(arrData);
41
Response.Write("<b>快速排序结果:<b><br>");
42
for(i=0; i<arrSortData.Length; i++)
43
{
44
Response.Write(arrSortData[i] + " ");
45
}
46
Response.Write("<hr>");
47
//输出插入排序结果
48
arrSortData = insertSort(arrData);
49
Response.Write("<b>插入排序结果:<b><br>");
50
for(i=0; i<arrSortData.Length; i++)
51
{
52
Response.Write(arrSortData[i] + " ");
53
}
54
Response.Write("<hr>");
55
//输出选择排序结果
56
arrSortData = selectSort(arrData);
57
Response.Write("<b>选择排序结果:<b><br>");
58
for(i=0; i<arrSortData.Length; i++)
59
{
60
Response.Write(arrSortData[i] + " ");
61
}
62
Response.Write("<hr>");
63
// 在此处放置用户代码以初始化页面
64
}
65
66
//冒泡排序
67
private int[] bubbleSort(int[] arrData)
68
{
69
if(arrData.Length <= 1)
70
{
71
return arrData;
72
}
73
bool bAllHaveSort = false;
74
for(int i=arrData.Length-1; i>0 && !bAllHaveSort; i--)
75
{
76
for(int j=0; j<i; j++)
77
{
78
bAllHaveSort = true;
79
if(arrData[j] > arrData[j+1])
80
{
81
int temp = arrData[j];
82
arrData[j] = arrData[j+1];
83
arrData[j+1] = temp;
84
bAllHaveSort = false;
85
}
86
}
87
}
88
return arrData;
89
}
90
//快速排序
91
private int[] quickSort(int[] arrData)
92
{
93
if(arrData.Length <= 1)
94
{
95
return arrData;
96
}
97
for(int i=0; i<arrData.Length-1; i++)
98
{
99
for(int j=i+1; j<arrData.Length; j++)
100
{
101
if(arrData[i]>arrData[j])
102
{
103
int temp = arrData[i];
104
arrData[i] = arrData[j];
105
arrData[j] = temp;
106
}
107
}
108
}
109
return arrData;
110
}
111
//插入排序
112
private int[] insertSort(int[] arrData)
113
{
114
if(arrData.Length <= 1)
115
{
116
return arrData;
117
}
118
for(int i=1; i<arrData.Length; i++)
119
{
120
int temp = arrData[i];
121
int j = i;
122
while(j>0 && arrData[j]<arrData[j-1])
123
{
124
arrData[j] = arrData[j-1];
125
j--;
126
}
127
arrData[j] = temp;
128
}
129
return arrData;
130
}
131
//选择排序
132
private int[] selectSort(int[] arrData)
133
{
134
if(arrData.Length <= 1)
135
{
136
return arrData;
137
}
138
for(int i=0; i<arrData.Length-1; i++)
139
{
140
int min = i;
141
for(int j=i+1; j<arrData.Length; j++)
142
{
143
if(arrData[min]>arrData[j])
144
{
145
min = j;
146
}
147
}
148
int temp = arrData[i];
149
arrData[i] = arrData[min];
150
arrData[min] = temp;
151
}
152
return arrData;
153
}
154
155
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
156
override protected void OnInit(EventArgs e)
157
{
158
//
159
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
160
//
161
InitializeComponent();
162
base.OnInit(e);
163
}
164
165
/**//// <summary>
166
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
167
/// 此方法的内容。
168
/// </summary>
169
private void InitializeComponent()
170
{
171
this.Load += new System.EventHandler(this.Page_Load);
172
173
}
174
#endregion
175
}
176
}
177

2

3

4

5

6

7

8

9

10

11

12

13



14


15

16

17

18



19

20



21

22

23



24

25

26

27



28

29

30

31

32

33

34

35



36

37

38

39

40

41

42

43



44

45

46

47

48

49

50

51



52

53

54

55

56

57

58

59



60

61

62

63

64

65

66

67

68



69

70



71

72

73

74

75



76

77



78

79

80



81

82

83

84

85

86

87

88

89

90

91

92



93

94



95

96

97

98



99

100



101

102



103

104

105

106

107

108

109

110

111

112

113



114

115



116

117

118

119



120

121

122

123



124

125

126

127

128

129

130

131

132

133



134

135



136

137

138

139



140

141

142



143

144



145

146

147

148

149

150

151

152

153

154

155


156

157



158

159

160

161

162

163

164

165


166

167

168

169

170



171

172

173

174

175

176

177
