1.冒泡排序

Delphi/Pascal code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
procedure BubbleSort(var x:array of integer);
var
  i,j,intTmp:integer;
begin
  for i:=0 to high(x) do
  begin
    for j:=0 to high(x)-1 do
    begin
      if x[j]>x[j+1then
      begin
        intTmp:=x[j];
        x[j]:=x[j+1];
        x[j+1]:=intTmp;
      end;
    end;
  end;
end;

2.选择排序

Delphi/Pascal code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
procedure SelectSort(var x:array of integer);
var
  i,j,k,intTmp:integer;
begin
  for i:=0 to high(x)-1 do
  begin
    intTmp:=x[i];
    k:=i;
    for j:=i+1 to high(x) do
    begin
      if intTmp>x[j] then
      begin
        k:=j;
        intTmp:=x[k];
      end;
    end;
    if k<>i then
    begin
      x[k]:=x[i];
      x[i]:=intTmp;
    end;
  end;
end;

3.插入排序

Delphi/Pascal code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
procedure InsertSort(var x:array of integer);
var
  i,j,intTmp:integer;
begin
  for i:=1 to high(x) do
  begin
    for j:=i downto 1 do
    begin
      if x[j-1]>x[j] then
      begin
        intTmp:=x[j-1];
        x[j-1]:=x[j];
        x[j]:=intTmp;
      end;
    end;
  end;
end;

4.希尔排序

Delphi/Pascal code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
procedure ShellSort(var x:array of integer);
var
  h,i,j,intTmp:integer;
begin
  h:=high(x) div 2;
  while h>0 do
  begin
    for i:=h to high(x) do
    begin
      j:=i;
      while (j>=h) and (x[j-h]>x[j]) do
      begin
        intTmp:=x[j-h];
        x[j-h]:=x[j];
        x[j]:=intTmp;
        j:=j-h;
      end;
    end;
    h:=h div 2;
  end;
end;

5.快速排序

Delphi/Pascal code

 

?

1
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
procedure QuickSort(var x:array of integer; L,R:integer);
var
  i,j,intTmp:integer;
begin
  if L<R then
  begin
    i:=L;
    j:=R;
    intTmp:=x[i];
    while i<j do
    begin
      while (i<j) and (x[j]>=intTmp) do
      begin
        j:=j-1;
      end;
      if i<j then x[i]:=x[j];
      while (i<j) and (x[i]<=intTmp) do
      begin
        i:=i+1;
      end;
      if i<j then x[j]:=x[i];
    end;
    x[i]:=intTmp;
    QuickSort(x,L,i-1);
    QuickSort(x,i+1,R);
  end;
end;

6.归并排序

Delphi/Pascal code

 

?

1
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
procedure Merge(var x,y:array of integer; L,M,R:integer);
var
  i,j:integer;
begin
  i:=L;
  j:=M+1;
  while (L<=M) and (j<=R) do
  begin
    if x[L]> x[j] then
    begin
      y[i]:=x[j];
      j:=j+1;
    end
    else
    begin
      y[i]:=x[L];
      L:=L+1;
    end;
    i:=i+1;
  end;
  while L<=M do
  begin
    y[i]:=x[L];
    i:=i+1;
    L:=L+1;
  end;
  while j<=R do
  begin
    y[i]:=x[j];
    i:=i+1;
    j:=j+1;
  end;
end;
 
procedure MergeSort(var x, y:TArrInt);
var
  intLength,intLen,intLen_m,i:integer;
  tmp:TArrInt;
begin
  intLength:=high(x)+1;
  intLen:=1;
 
  while intLen<intLength do
  begin
    intLen_m:=intLen;
    intLen:=intLen*2;
    i:=0;
    while i+intLen<intLength do
    begin
      Merge(x,y,i,i+intLen_m-1,i+intLen-1);
      i:=i+intLen;
    end;
    if i+intLen_m<intLength then
    begin
      Merge(x,y,i,i+intLen_m-1,intLength-1);
    end;
 
    tmp:=x;
    x:=y;
    y:=tmp;
  end;
end;

7.堆排序

Delphi/Pascal code

 

?

1
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
procedure HeapAdjust(var x:array of integer; i,intLen:integer);
var
  intTmp,intChild:integer;
begin
  intTmp:=x[i];
  intChild:=2*i+1;
  while intChild<intLen do
  begin
    if (intChild+1<intLen) and (x[intChild]<x[intChild+1]) then
    begin
      intChild:=intChild+1;
    end;
    if x[i]<x[intChild] then
    begin
      x[i]:=x[intChild];
      i:=intChild;
      intChild:=2*i+1;
    end
    else
    begin
      break;
    end;
    x[i]:=intTmp;
  end;
end;
 
procedure BuildHeap(var x:array of integer);
var
  i:integer;
begin
  for i:=high(x) div 2 downto 0 do
  begin
    HeapAdjust(x,i,High(x)+1);
  end;
end;
 
procedure HeapSort(var x:array of integer);
var
  i,intTmp:integer;
begin
  BuildHeap(x);
  for i:=high(x) downto 0 do
  begin
    intTmp:=x[i];
    x[i]:=x[0];
    x[0]:=intTmp;
    HeapAdjust(x,0,i);
  end;
end;
 
参考:http://m.blog.csdn.net/blog/fghydx/46401781

Delphi常用排序的更多相关文章

  1. Delphi常用字符串函数

    Delphi常用字符串函数   一.字符转换函数1.ord(input[i])返回字符表达式 input 左端起第 I 字符的ASCII 码值.2.CHAR()将ASCII 码转换为字符.如果没有输入 ...

  2. Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法

    Java 常用排序算法/程序员必须掌握的 8大排序算法 本文由网络资料整理转载而来,如有问题,欢迎指正! 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排 ...

  3. Delphi常用系统函数总结

    Delphi常用系统函数总结 字符串处理函数 Unit System 函数原型 function Concat(s1 [, s2,..., sn]: string): string; 说明 与 S : ...

  4. 转载部长一篇大作:常用排序算法之JavaScript实现

    转载部长一篇大作:常用排序算法之JavaScript实现 注:本文是转载实验室同门王部长的大作,找实习找工作在即,本文颇有用处!原文出处:http://www.cnblogs.com/ywang172 ...

  5. Java 常用排序算法/程序员必须掌握的 8大排序算法

    Java 常用排序算法/程序员必须掌握的 8大排序算法 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分配 ...

  6. (转载)delphi 常用函数(数学)

    delphi 常用函数(数学) Delphi中怎么将实数取整? floor 和 ceil 是 math unit 里的函数,使用前要先 Uses Math.trunc 和 round 是 system ...

  7. 常用排序算法的python实现和性能分析

    常用排序算法的python实现和性能分析 一年一度的换工作高峰又到了,HR大概每天都塞几份简历过来,基本上一天安排两个面试的话,当天就只能加班干活了.趁着面试别人的机会,自己也把一些基础算法和一些面试 ...

  8. Delphi 常用函数(数学函数)round、trunc、ceil和floor

    源:Delphi 常用函数(数学函数)round.trunc.ceil和floor Delphi 常用函数(数学) Delphi中怎么将实数取整? floor 和 ceil 是 math unit 里 ...

  9. Delphi 常用API 函数

    Delphi 常用API 函数 AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小 AnyPopup 判断屏幕上是否存在任何弹出式窗口 ArrangeIconic ...

随机推荐

  1. 也许是关于C#的一些常见误区

    写这点东西主要是看到知乎上有人在讨论相关的问题,但是有不少人都在说一些不严谨,甚至是完全错误 但是流传甚广的东西,甚至是一些大神都在说,以下根据我的回答总结.    一个很常见又很低级的误区是:认为引 ...

  2. 在Silverlight中使用async/await

    现在 async/await 大行其道,确实,有了 async/await ,异步编程真是简单多了,个人觉得 async/await 的出现,给开发者还来的方便,绝不亚于当年 linq 的出现. 但要 ...

  3. 正则表达式工具类,正则表达式封装,Java正则表达式

    正则表达式工具类 正则表达式封装 Java正则表达式 >>>>>>>>>>>>>>>>>>& ...

  4. X86(32位)与X64(64位)有什么区别,如何选择对应的操作系统和应用程序?

    X86就是我们一般用的32位的系统,指针长度为32位(386起):X64就是64位的系统,指针长度为64位. 选择硬件对应的软件,建议通过以下三条考虑:1.64位操作系统相对32位操作系统理论上性能会 ...

  5. MYSQL之高级查询

    PHP高级查询 分组查询.联合查询.连接查询.子查询 版权声明:本文为博主原创文章,未经博主允许不得转载.

  6. [Mime] MediaTypes--电子邮件类型类 (转载)

    点击下载 MediaTypes.rar 这个类是关于 电子邮件类型类的操作,在发送电子邮件是规定以什么样的格式发送,Xml,HTML,文本等方式1.电子邮件类型帮助类,Xml格式,HTML格式等看下面 ...

  7. QVW中实现日期区间的选择功能!

    QV在日期的选择上不是很灵活,日期区段的选择可以在列表框中直接用鼠标拖拉区段,如果跨周期比较长了还是不是很方便啦. 下面介绍的方式是完全实现了起始日期的选择功能. 注:日期这个字段在抽取的时候一定要格 ...

  8. Shell编程之Shift的用法

    位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1. 非常 ...

  9. angular2 组件之间通讯-使用服务通讯模式 2016.10.27 基于正式版ng2

    工作中用到ng2的组件通讯 奈何官方文档言简意赅 没说明白 自己搞明白后 整理后分享下 rxjs 不懂的看这篇文章 讲很详细 http://www.open-open.com/lib/view/ope ...

  10. 使用Ubuntu 新建vpn过程

    1.更新软件源      sudo apt-get update   2.安装pip    sudo apt-get install python-pip   3.安装shadowsocks    s ...