|
Posted on 2006-12-29 19:37 路易 阅读(221) 评论(0) 编辑 收藏 所属分类: AJAX
1
冒泡排序
2
3
using
System;
4
5
namespace
BubbleSorter
6
7
{
public
class
BubbleSorter
8
9
{
public
void
Sort(
int
[] list)
10
11
{
int
i,j,temp;
12
13
bool
done
=
false
;
14
15
j
=
1
;
16
17
while
((j<list.Length)
&&
(
!
done))
18
19
{ done
=
true
;
20
21
for
(i
=
0
;i<list.Length
-
j;i
++
)
22
23
{
24
25
if
(list[i]>list[i
+
1
])
26
27
{
28
29
done
=
false
;
30
31
temp
=
list[i];
32
33
list[i]
=
list[i
+
1
];
34
35
list[i
+
1
]
=
temp;
36
37
}
}
38
39
j
++
; }
40
41
}
}
42
43
public
class
MainClass
44
45
{
public
static
void
Main()
46
47
{
48
49
int
[] iArrary
=
new
int
[]
{
1
,
5
,
13
,
6
,
10
,
55
,
99
,
2
,
87
,
12
,
34
,
75
,
33
,
47
}
;
50
51
BubbleSorter sh
=
new
BubbleSorter();
52
53
sh.Sort(iArrary);
54
55
for
(
int
m
=
0
;m<iArrary.Length;m
++
)
56
57
Console.Write(
"
{0}
"
,iArrary[m]);
58
59
Console.WriteLine();
60
61
}
}
62
63
}
64
65
66
67
68
选择排序
69
70
using
System;
71
72
73
namespace
SelectionSorter
74
75
{
public
class
SelectionSorter
76
77
{
private
int
min;
78
79
public
void
Sort(
int
[] list)
80
81
{
for
(
int
i
=
0
;i<list.Length
-
1
;i
++
)
82
83
{ min
=
i;
84
85
for
(
int
j
=
i
+
1
;j<list.Length;j
++
)
86
87
{
if
(list[j]<list[min])
88
89
min
=
j;
90
91
}
92
93
int
t
=
list[min];
94
95
list[min]
=
list[i];
96
97
list[i]
=
t;
98
99
}
}
100
101
}
102
103
public
class
MainClass
104
105
{
public
static
void
Main()
106
107
{
108
109
int
[] iArrary
=
new
int
[]
{
1
,
5
,
3
,
6
,
10
,
55
,
9
,
2
,
87
,
12
,
34
,
75
,
33
,
47
}
;
110
111
SelectionSorter ss
=
new
SelectionSorter();
112
113
ss.Sort(iArrary);
114
115
for
(
int
m
=
0
;m<iArrary.Length;m
++
)
116
117
Console.Write(
"
{0}
"
,iArrary[m]);
118
119
Console.WriteLine();
120
121
}
}
122
123
}
124
125
126
127
128
插入排序
129
130
using
System;
131
132
namespace
InsertionSorter
133
134
{
public
class
InsertionSorter
135
136
{
public
void
Sort(
int
[] list)
137
138
{
for
(
int
i
=
1
;i<list.Length;i
++
)
139
140
{
int
t
=
list[i];
141
142
int
j
=
i;
143
144
while
((j>
0
)
&&
(list[j
-
1
]>t))
145
146
{ list[j]
=
list[j
-
1
];
147
148
--
j;
149
150
}
151
152
list[j]
=
t; }
153
154
}
155
156
}
157
158
public
class
MainClass
159
160
{
public
static
void
Main()
161
162
{
163
164
int
[] iArrary
=
new
int
[]
{
1
,
13
,
3
,
6
,
10
,
55
,
98
,
2
,
87
,
12
,
34
,
75
,
33
,
47
}
;
165
166
InsertionSorter ii
=
new
InsertionSorter();
167
168
ii.Sort(iArrary);
169
170
for
(
int
m
=
0
;m<iArrary.Length;m
++
)
171
172
Console.Write(
"
{0}
"
,iArrary[m]);
173
174
Console.WriteLine();
175
176
}
}
177
178
}
179
180
181
182
183
希尔排序
184
185
希尔排序是将组分段,进行插入排序.
186
187
using
System;
188
189
namespace
ShellSorter
190
191
{
192
193
public
class
ShellSorter
194
195
{
196
197
public
void
Sort(
int
[] list)
198
199
{
200
201
int
inc;
202
203
for
(inc
=
1
;inc<
=
list.Length
/
9
;inc
=
3
*
inc
+
1
);
204
205
for
(;inc>
0
;inc
/=
3
)
206
207
{
208
209
for
(
int
i
=
inc
+
1
;i<
=
list.Length;i
+=
inc)
210
211
{
212
213
int
t
=
list[i
-
1
];
214
215
int
j
=
i;
216
217
while
((j>inc)
&&
(list[j
-
inc
-
1
]>t))
218
219
{
220
221
list[j
-
1
]
=
list[j
-
inc
-
1
];
222
223
j
-=
inc;
224
225
}
226
227
list[j
-
1
]
=
t;
228
229
}
}
230
231
}
}
232
233
public
class
MainClass
234
235
{
public
static
void
Main()
236
237
{
238
239
int
[] iArrary
=
new
int
[]
{
1
,
5
,
13
,
6
,
10
,
55
,
99
,
2
,
87
,
12
,
34
,
75
,
33
,
47
}
;
240
241
ShellSorter sh
=
new
ShellSorter();
242
243
sh.Sort(iArrary);
244
245
for
(
int
m
=
0
;m<iArrary.Length;m
++
)
246
247
Console.Write(
"
{0}
"
,iArrary[m]);
248
249
Console.WriteLine();
250
251
}
}
252
253
}
http://www.uml.org.cn/net/200603242.htm
|