Приклади задач

Приклади задач в яких використовується сортування шляхом злиття
1.)
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Button1: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  k,i,j,x,l,r:integer;
  a,b:array of integer;

implementation

{$R *.dfm}

procedure merge(L,x,R:Integer);
  begin
    i:=L;
      x:=(L+R) div 2;
    j:=x+1;
    k:=0;
    while (i<=x) and (j<=R) do
    begin
      if a[i]>a[j] then
        begin
            b[k]:=a[j];
            j:=j+1;
        end
      else
        begin
            b[k]:=a[i];
            i:=i+1;
        end;
      k:=k+1;
    end;
    if i<=x then      
      for j:=i to x do
        begin
            b[k]:=a[j];
            k:=k+1;
        end
    else 
      for i:=j to R do
        begin
            b[k]:=a[i];
            k:=k+1;
        end;
    Move(b[0], a[L], k*SizeOf(integer));
  end;

procedure sort(L,R:Integer);
  begin
    if L>=R then showmessage('No. L must be < R');
    x:=(L+R) div 2; 
    sort(L, x);      
    sort(x+1, R);    
    merge(L, x, R);  
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  l:=strtoint(edit1.text);
  r:=strtoint(edit2.text);
  SetLength(a, r);
  SetLength(b, Length(a));
  Randomize;
  For i:=1 to r do
    begin
      A[i]:=Random(1000);
        Memo1.text:=inttostr(a[i]);
    end;
  sort(0, Length(a)-1);
  SetLength(b, 0);
  Memo2.text:=inttostr(a[i]);
end;
end.


2.) 
var
  left,right,k,i,x,j,m:integer;
    mas,a:array of integer;

implementation

begin
  left:=0;
  right:=0;
  k:=0;
  x:=0;
    left:=strtoint(edit1.text);
    right:=strtoint(edit2.text);
    SetLength(a, right);
    SetLength(mas, right);
    Randomize;
    For i := 1 To right Do begin
      A[i] := Random(1000);
      StringGrid1.Cells[i,0]:=FloatToStr(a[i]);
    end;
    for i:=1 to right do
  begin
    x:=(a[i]+a[right-i]) div 2;
    if a[i]>x then
        x:=a[i]
    else
        if mas[1]=0 then
            mas[j]:=x
        else
            mas[j+1]:=x;
  end;
    for m:=1 to right do
      StringGrid2.Cells[m, j] := FloatToStr(A[m]);

end;

3.)
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Button1: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  k,i,j,x,l,r:integer;
  a,b:array of integer;

implementation

{$R *.dfm}


procedure merge(L,x,R:Integer);

Комментариев нет:

Отправить комментарий