помогите найти ошибку в проге, правда она немного корявая. но все же
Код | unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls;
type TForm1 = class(TForm) Memo1: TMemo; Edit1: TEdit; Button1: TButton; Edit2: TEdit; Memo2: TMemo; Button2: TButton; Edit3: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; const n = 20000; type mas= array[1..n] of Longint; type buf= array[1..5]of longint; var Form1: TForm1; i,j,k: Longint; ar,m,b: mas; s:Longint; implementation
{$R *.dfm}
procedure init(var a:mas; N:Longint); var i:Longint; begin randomize; for i:=1 to n do a[i]:=random(100); end;
procedure insertsort(var a:mas); var i,j: Longint; x: Longint; begin for i:=2 to 5 do begin x:=a[i];j:=i-1; while((x<a[j])and (j>0)) do begin a[j+1]:=a[j]; dec(j); end; a[j+1]:=x; end; end;
procedure med(var a:mas;count:Longint); var s,t,i:Longint; begin s:=0; t:=0; for i:=1 to count do begin for t:=(5*i-4) to (5*i) do begin inc(s); b[s]:=a[t]; end; insertsort(b); m[i]:=b[3]; s:=0; end; end;
function partition(var a:mas;p:Longint;r:Longint;x:Longint):Longint; var temp:longint; i,j:longint; begin i:=p; j:=r; while i<j do begin repeat dec(j); until a[j]<x; repeat inc(i); until a[i]>x; if i<j then begin temp:=a[i];a[i]:=a[j];a[j]:=temp; end else partition:=j; end; end;
function select(var a:mas;index:longint):Longint; var count,m_of_m:Longint; k:Longint; stat:longint; begin if (sizeof(a) mod 5)<> 0 then count:=(n div 5)+1 else count:=n div 5; med(a,n); m_of_m:=select(m,(count+1)div 2); k:=partition(a,1,n,m_of_m); if index<=k then stat:=select(a,index) else stat:=select(a,index-k); select:=a[stat]; end; procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Clear; j:=StrToInt(Edit1.Text); init(ar,j); for i:=1 to j do memo1.Lines.Add(inttostr(ar[i])); end;
procedure TForm1.Button2Click(Sender: TObject); var x:Longint; begin s:=StrToInt(Edit2.Text); for i:=1 to j do memo2.Lines.Add(inttostr(ar[i])); x:=select(ar,s); edit3.Text:=inttostr(x); end;
end.
| |