Vlozte za implementation {$R *.lfm}

Var g: TTreeView;

Pozmnente proceduru quick

procedure quick (r, s: integer; t: TTreeNode);
...
    t := g.Items.AddChild (t, IntToStr (r) + '..' + IntToStr (s));
...
    if r<k then quick(r,k, t);
    if i<s then quick(i,s, t);

Upravte TForm1.Button1Click

procedure TForm1.Button1Click(Sender: TObject);
begin
   fill;
   g := TreeView1;
   quick (1, n, nil);
end;

Cely soubor

unit Unit1;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls;
 
type
 
  { TForm1 }
 
  TForm1 = class(TForm)
    Button1: TButton;
    TreeView1: TTreeView;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.lfm}
 
Var g: TTreeView;
 
const n=31;
 
var a: array [1..n] of integer;
 
procedure aswap (i, j: integer);
var t : integer;
begin
     t := a[i];
     a[i] := a[j];
     a[j] := t;
end;
 
procedure quick (r, s: integer; t: TTreeNode);
var i, k, x: integer;
begin
    t := g.Items.AddChild (t, IntToStr (r) + '..' + IntToStr (s));
    i := r;
    k := s;
    x := a[(i+k) div 2];
    repeat
         while a[i] < x do i:= i+1;
         while a[k] > x do k:=k-1;
         if i<=k then begin
                       aswap (i,k);
                       i:=i+1;
                       k:=k-1;
                    end;
    until i>k;
    if r<k then quick(r,k, t);
    if i<s then quick(i,s, t);
 
end;
 
procedure fill;
var i: integer;
begin
  randomize;
  for i:= 1 to n do
    a[i]:= random(100);
end;
 
procedure print;
var i: integer;
begin
  for i:= 1 to n do
    write(a[i],' ');
  writeln;
end;
 
procedure check;
var i:integer;
    ok: boolean;
begin
    ok:=true;
    for i:=1 to n-1 do
      if a[i] > a[i+1]then
         ok:=false;
    if not ok then
         writeln('Chyba');
end;
 
{ TForm1 }
 
procedure TForm1.Button1Click(Sender: TObject);
begin
   fill;
   g := TreeView1;
   quick (1, n, nil);
end;
 
end.
 
quicksortwin.txt · Last modified: 2014/04/24 15:09 by 147.32.8.115
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki