几种排序

Posted 狼行神码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了几种排序相关的知识,希望对你有一定的参考价值。

选择排序的实例:

代码:

  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
  8 
  9 type
 10   TForm1 = class(TForm)
 11     GroupBox1: TGroupBox;
 12     Label1: TLabel;
 13     Edit1: TEdit;
 14     Label2: TLabel;
 15     Label3: TLabel;
 16     Label4: TLabel;
 17     Label5: TLabel;
 18     Label6: TLabel;
 19     Label7: TLabel;
 20     Label8: TLabel;
 21     Label9: TLabel;
 22     Label10: TLabel;
 23     Edit2: TEdit;
 24     Edit3: TEdit;
 25     Edit4: TEdit;
 26     Edit5: TEdit;
 27     Edit6: TEdit;
 28     Edit7: TEdit;
 29     Edit8: TEdit;
 30     Edit9: TEdit;
 31     Edit10: TEdit;
 32     Button1: TButton;
 33     GroupBox2: TGroupBox;
 34     Label11: TLabel;
 35     Label12: TLabel;
 36     Label13: TLabel;
 37     Label14: TLabel;
 38     Label15: TLabel;
 39     Label16: TLabel;
 40     Label17: TLabel;
 41     Label18: TLabel;
 42     Label19: TLabel;
 43     Label20: TLabel;
 44     Edit11: TEdit;
 45     Edit12: TEdit;
 46     Edit13: TEdit;
 47     Edit14: TEdit;
 48     Edit15: TEdit;
 49     Edit16: TEdit;
 50     Edit17: TEdit;
 51     Edit18: TEdit;
 52     Edit19: TEdit;
 53     Edit20: TEdit;
 54     Button2: TButton;
 55     procedure Button1Click(Sender: TObject);
 56     procedure Button2Click(Sender: TObject);
 57   private
 58     { Private declarations }
 59   public
 60     { Public declarations }
 61   end;
 62 
 63 var
 64   Form1: TForm1;
 65 
 66 implementation
 67 
 68 {$R *.dfm}
 69 var
 70   MyArray: array [1..10] of integer;
 71   i,j,k,temp:integer;
 72 procedure TForm1.Button1Click(Sender: TObject);  //排序前 建立数组并对数组进行赋值
 73 begin
 74   for i := 1 to 10 do
 75     begin
 76       MyArray[i] := Random(100);
 77     end;
 78 
 79   Edit1.Text := IntToStr(MyArray[1]);
 80   Edit2.Text := IntToStr(MyArray[2]);
 81   Edit3.Text := IntToStr(MyArray[3]);
 82   Edit4.Text := IntToStr(MyArray[4]);
 83   Edit5.Text := IntToStr(MyArray[5]);
 84   Edit6.Text := IntToStr(MyArray[6]);
 85   Edit7.Text := IntToStr(MyArray[7]);
 86   Edit8.Text := IntToStr(MyArray[8]);
 87   Edit9.Text := IntToStr(MyArray[9]);
 88   Edit10.Text := IntToStr(MyArray[10]);
 89 end;
 90 
 91 procedure TForm1.Button2Click(Sender: TObject);  //选择排序
 92 begin
 93   for i:=1 to 10 do
 94   begin
 95     temp:=MyArray[i];
 96     k:=i;
 97     for j:=i+1 to 10 do
 98     begin
 99       if Temp>MyArray[j] then
100       begin
101         k:=j;
102         Temp:=MyArray[k];
103       end;
104     end;
105     if k<>i then
106     begin
107       MyArray[k]:=MyArray[i];
108       MyArray[i]:=Temp;
109     end;
110   end;
111   Edit11.Text := IntToStr(MyArray[1]);
112   Edit12.Text := IntToStr(MyArray[2]);
113   Edit13.Text := IntToStr(MyArray[3]);
114   Edit14.Text := IntToStr(MyArray[4]);
115   Edit15.Text := IntToStr(MyArray[5]);
116   Edit16.Text := IntToStr(MyArray[6]);
117   Edit17.Text := IntToStr(MyArray[7]);
118   Edit18.Text := IntToStr(MyArray[8]);
119   Edit19.Text := IntToStr(MyArray[9]);
120   Edit20.Text := IntToStr(MyArray[10]);
121 end;
122 
123 end.

 

 

其他排序的代码:

 

  1 1.冒泡排序
  2 Delphi/Pascal code
  3 ?
  4 1
  5 2
  6 3
  7 4
  8 5
  9 6
 10 7
 11 8
 12 9
 13 10
 14 11
 15 12
 16 13
 17 14
 18 15
 19 16
 20 17
 21 procedure BubbleSort(var x:array of integer);
 22 var
 23   i,j,intTmp:integer;
 24 begin
 25   for i:=0 to high(x) do
 26   begin
 27     for j:=0 to high(x)-1 do
 28     begin
 29       if x[j]>x[j+1] then
 30       begin
 31         intTmp:=x[j];
 32         x[j]:=x[j+1];
 33         x[j+1]:=intTmp;
 34       end;
 35     end;
 36   end;
 37 end;
 38 
 39 
 40 
 41 2.选择排序
 42 Delphi/Pascal code
 43 ?
 44 1
 45 2
 46 3
 47 4
 48 5
 49 6
 50 7
 51 8
 52 9
 53 10
 54 11
 55 12
 56 13
 57 14
 58 15
 59 16
 60 17
 61 18
 62 19
 63 20
 64 21
 65 22
 66 23
 67 procedure SelectSort(var x:array of integer);
 68 var
 69   i,j,k,intTmp:integer;
 70 begin
 71   for i:=0 to high(x)-1 do
 72   begin
 73     intTmp:=x[i];
 74     k:=i;
 75     for j:=i+1 to high(x) do
 76     begin
 77       if intTmp>x[j] then
 78       begin
 79         k:=j;
 80         intTmp:=x[k];
 81       end;
 82     end;
 83     if k<>i then
 84     begin
 85       x[k]:=x[i];
 86       x[i]:=intTmp;
 87     end;
 88   end;
 89 end;
 90 
 91 
 92 
 93 3.插入排序
 94 Delphi/Pascal code
 95 ?
 96 1
 97 2
 98 3
 99 4
100 5
101 6
102 7
103 8
104 9
105 10
106 11
107 12
108 13
109 14
110 15
111 16
112 17
113 procedure InsertSort(var x:array of integer);
114 var
115   i,j,intTmp:integer;
116 begin
117   for i:=1 to high(x) do
118   begin
119     for j:=i downto 1 do
120     begin
121       if x[j-1]>x[j] then
122       begin
123         intTmp:=x[j-1];
124         x[j-1]:=x[j];
125         x[j]:=intTmp;
126       end;
127     end;
128   end;
129 end;
130 
131 
132 
133 4.希尔排序
134 Delphi/Pascal code
135 ?
136 1
137 2
138 3
139 4
140 5
141 6
142 7
143 8
144 9
145 10
146 11
147 12
148 13
149 14
150 15
151 16
152 17
153 18
154 19
155 20
156 21
157 procedure ShellSort(var x:array of integer);
158 var
159   h,i,j,intTmp:integer;
160 begin
161   h:=high(x) div 2;
162   while h>0 do
163   begin
164     for i:=h to high(x) do
165     begin
166       j:=i;
167       while (j>=h) and (x[j-h]>x[j]) do
168       begin
169         intTmp:=x[j-h];
170         x[j-h]:=x[j];
171         x[j]:=intTmp;
172         j:=j-h;
173       end;
174     end;
175     h:=h div 2;
176   end;
177 end;
178 
179 
180 
181 5.快速排序
182 Delphi/Pascal code
183 ?
184 1
185 2
186 3
187 4
188 5
189 6
190 7
191 8
192 9
193 10
194 11
195 12
196 13
197 14
198 15
199 16
200 17
201 18
202 19
203 20
204 21
205 22
206 23
207 24
208 25
209 26
210 27
211 procedure QuickSort(var x:array of integer; L,R:integer);
212 var
213   i,j,intTmp:integer;
214 begin
215   if L<R then
216   begin
217     i:=L;
218     j:=R;
219     intTmp:=x[i];
220     while i<j do
221     begin
222       while (i<j) and (x[j]>=intTmp) do
223       begin
224         j:=j-1;
225       end;
226       if i<j then x[i]:=x[j];
227       while (i<j) and (x[i]<=intTmp) do
228       begin
229         i:=i+1;
230       end;
231       if i<j then x[j]:=x[i];
232     end;
233     x[i]:=intTmp;
234     QuickSort(x,L,i-1);
235     QuickSort(x,i+1,R);
236   end;
237 end;
238 
239 
240 
241 6.归并排序
242 Delphi/Pascal code
243 ?
244 1
245 2
246 3
247 4
248 5
249 6
250 7
251 8
252 9
253 10
254 11
255 12
256 13
257 14
258 15
259 16
260 17
261 18
262 19
263 20
264 21
265 22
266 23
267 24
268 25
269 26
270 27
271 28
272 29
273 30
274 31
275 32
276 33
277 34
278 35
279 36
280 37
281 38
282 39
283 40
284 41
285 42
286 43
287 44
288 45
289 46
290 47
291 48
292 49
293 50
294 51
295 52
296 53
297 54
298 55
299 56
300 57
301 58
302 59
303 60
304 61
305 62
306 procedure Merge(var x,y:array of integer; L,M,R:integer);
307 var
308   i,j:integer;
309 begin
310   i:=L;
311   j:=M+1;
312   while (L<=M) and (j<=R) do
313   begin
314     if x[L]> x[j] then
315     begin
316       y[i]:=x[j];
317       j:=j+1;
318     end
319     else
320     begin
321       y[i]:=x[L];
322       L:=L+1;
323     end;
324     i:=i+1;
325   end;
326   while L<=M do
327   begin
328     y[i]:=x[L];
329     i:=i+1;
330     L:=L+1;
331   end;
332   while j<=R do
333   begin
334     y[i]:=x[j];
335     i:=i+1;
336     j:=j+1;
337   end;
338 end;
339  
340 procedure MergeSort(var x, y:TArrInt);
341 var
342   intLength,intLen,intLen_m,i:integer;
343   tmp:TArrInt;
344 begin
345   intLength:=high(x)+1;
346   intLen:=1;
347  
348   while intLen<intLength do
349   begin
350     intLen_m:=intLen;
351     intLen:=intLen*2;
352     i:=0;
353     while i+intLen<intLength do
354     begin
355       Merge(x,y,i,i+intLen_m-1,i+intLen-1);
356       i:=i+intLen;
357     end;
358     if i+intLen_m<intLength then
359     begin
360       Merge(x,y,i,i+intLen_m-1,intLength-1);
361     end;
362  
363     tmp:=x;
364     x:=y;
365     y:=tmp;
366   end;
367 end;
368 
369 
370 
371 7.堆排序
372 Delphi/Pascal code
373 ?
374 1
375 2
376 3
377 4
378 5
379 6
380 7
381 8
382 9
383 10
384 11
385 初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

hadoop的mapreduce常见算法案例有几种

一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式

一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式

Java实现几种排序

几种排序方式的java实现(01:插入排序,冒泡排序,选择排序,快速排序)