Delphi常用排序

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

时间: 2024-08-01 07:42:31

Delphi常用排序的相关文章

Delphi常用函数

Abort                      函数 引起放弃的意外处理 Abs(real)                  函数 绝对值函数 AddExitProc                函数 将一过程添加到运行时库的结束过程表中 Addr(variant)              函数 返回指定对象的地址 AdjustLineBreaks           函数 将给定字符串的行分隔符调整为CR/LF序列 AllocMem                   函数 在堆栈

Delphi常用字符串函数

Delphi常用字符串函数 一.字符转换函数1.ord(input[i])返回字符表达式 input 左端起第 I 字符的ASCII 码值.2.CHAR()将ASCII 码转换为字符.如果没有输入0 ~ 255 之间的ASCII 码值,CHAR() 返回NULL .3.LOWER()和UPPER()LOWER()将字符串全部转为小写:UPPER()将字符串全部转为大写.4.STR()把数值型数据转换为字符型数据.STR (<float_expression>[,length[, <dec

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

Java 常用排序算法/程序员必须掌握的 8大排序算法 本文由网络资料整理转载而来,如有问题,欢迎指正! 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分配排序(基数排序) 所需辅助空间最多:归并排序 所需辅助空间最少:堆排序 平均速度最快:快速排序 不稳定:快速排序,希尔排序,堆排序. 先来看看 8种排序之间的关系: 1.直接插入排序 (1)基本思想:在要排序的一组数中,假设前面(n-1)[n>=2]

常用排序算法比较与分析

一.常用排序算法简述 下面主要从排序算法的基本概念.原理出发,分别从算法的时间复杂度.空间复杂度.算法的稳定性和速度等方面进行分析比较.依据待排序的问题大小(记录数量 n)的不同,排序过程中需要的存储器空间也不同,由此将排序算法分为两大类:[内排序].[外排序]. 内排序:指排序时数据元素全部存放在计算机的随机存储器RAM中. 外排序:待排序记录的数量很大,以致内存一次不能容纳全部记录,在排序过程中还需要对外存进行访问的排序过程. 先了解一下常见排序算法的分类关系(见图1-1) 图1-1 常见排

delphi 常用api

Delphi 常用API 函数 AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小 AnyPopup 判断屏幕上是否存在任何弹出式窗口 ArrangeIconicWindows 排列一个父窗口的最小化子窗口 AttachThreadInput 连接线程输入函数 BeginDeferWindowPos 启动构建一系列新窗口位置的过程 BringWindowToTop 将指定的窗口带至窗口列表顶部 CascadeWindows 以层叠方式排列窗口 ChildWi

七种常用排序算法

七种常用排序算法 一.常见排序算法一览: 时间复杂度: 是一个函数,它定量描述了该算法的运行时间. 空间复杂度:一个算法在运行过程中临时占用存储空间大小的量度. 稳定性:保证排序前2个相等的数其在序列的前后位置顺序和排序后它们两个的前后位置顺序相同就稳定,反之不稳定. 视觉直观感受 7 种常用的排序算法 二.算法C#实现: 1. 直接插入排序: using System; using System.Collections.Generic; using System.Linq; using Sys

常用排序算法之——归并排序

归并排序的原理: 如果数组的元素个数大于1,则: 将数组平均分为两部分: 左边的数组归并排序:递归 右边的数组归并排序:递归 将两个各自有序的数组合并,需要一个额外的辅助数组,暂时保存合并结果:返回 否则,数组元素个数为1时,已经有序:直接返回. 稳定排序.时间复杂度在最坏.最好.平均情况下都为O(N lgN),空间复杂度为O(N). 代码: 1 #include <iostream> 2 using namespace std; 3 4 template<typename T>

常用排序算法之——快速排序

快速排序的原理: 首先找一个标兵值,等于某一个元素值:遍历数组,将数组分为小于标兵值和大于标兵值的两部分:然后分别对两个部分采用快速排序,递归. 分开数组时,维持一个指针,指向已找到小部分的最后一个元素:一个指针用于遍历. 不稳定排序算法.当数组已经有序时,时间复杂度最差,为O(N2),平均.最优情况下都为O(N lgN). 代码如下: 1 #include <iostream> 2 using namespace std; 3 4 template<typename T> 5 v

javascript常用排序算法实现

毕业后,由于工作中很少需要自已去写一些排序,所以那些排序算法都忘得差不多了,不过排序是最基础的算法,还是不能落下啦,于是找了一些资料,然后用Javascript实现了一些常用的算法,具体代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>