⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 分而治之方法还可以用于实现另一种完全不同的排序方法
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;
    Edit12: TEdit;
    Edit13: TEdit;
    Edit14: TEdit;
    Edit15: TEdit;
    Edit16: TEdit;
    Edit17: TEdit;
    Edit18: TEdit;
    Edit19: TEdit;
    Edit20: TEdit;
    Button2: TButton;
    ListBox1: TListBox;
    procedure Button2Click(Sender: TObject);
  private
    procedure sort(l, r: integer);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  a: array[1..10] of integer;

implementation

{$R *.dfm}

procedure TForm1.sort(l, r: integer);
var i, j, mid,x: integer;

begin
  i := l; j := r; //mid := a[(l + r) div 2]; {将当前序列在中间位置的数定义为中间数}
  mid := a[l];
  repeat
    while a[i] < mid do inc(i); {在左半部分寻找比中间数大的数}
    while mid < a[j] do dec(j); {在右半部分寻找比中间数小的数}
    if i <= j then
    begin {若找到一组与排序目标不一致的数对则交换它们}
      x:=a[i];
      a[i]:=a[j];
      a[j]:=x;
      inc(i);
      dec(j); {继续找}
      listbox1.Items.Add(inttostr(a[1])+','+inttostr(a[2])+','+inttostr(a[3])+','+inttostr(a[4])+','+inttostr(a[5])+','+inttostr(a[6])+','+inttostr(a[7])+','+inttostr(a[8])+','+inttostr(a[9])+','+inttostr(a[10])+',');
    end;
  until i > j;
  if l < j then sort(l, j); {若未到两个数的边界,则递归搜索左右区间}
  if i < r then sort(i, r);

end;


procedure TForm1.Button2Click(Sender: TObject);
begin
  a[1] := strtoint(Edit1.text);
  a[2] := strtoint(Edit2.text);
  a[3] := strtoint(Edit3.text);
  a[4] := strtoint(Edit4.text);
  a[5] := strtoint(Edit5.text);
  a[6] := strtoint(Edit6.text);
  a[7] := strtoint(Edit7.text);
  a[8] := strtoint(Edit8.text);
  a[9] := strtoint(Edit9.text);
  a[10] := strtoint(Edit10.text);
  sort(1, 10);
  
  Edit11.text := inttostr(a[1]);
  Edit12.text := inttostr(a[2]);
  Edit13.text := inttostr(a[3]);
  Edit14.text := inttostr(a[4]);
  Edit15.text := inttostr(a[5]);
  Edit16.text := inttostr(a[6]);
  Edit17.text := inttostr(a[7]);
  Edit18.text := inttostr(a[8]);
  Edit19.text := inttostr(a[9]);
  Edit20.text := inttostr(a[10]);

end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -