Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
3768.doc
Скачиваний:
111
Добавлен:
13.11.2022
Размер:
15.23 Mб
Скачать

Приложение 2. Процедура на языке Turbo Delphi, имитирующая механизм сортировки элементов массива простым выбором

var

Form1: TForm1;

var n:integer;

a:array[1..100] of integer;

min,nmin,i,j:integer;

implementation

{$R *.dfm}

procedure TForm1.FormDblClick(Sender: TObject);

var s:string; var i:integer;

begin

Button1.Enabled:=False; Button2.Enabled:=False;

s:=InputBox('Введите размер массива', '','');

n:=StrToInt(s);

StringGrid1.ColCount:=n+1;

StringGrid2.ColCount:=n+1;

for i:=1 to n do

begin

StringGrid1.Cells[i,0]:=IntToStr(i);

StringGrid2.Cells[i,0]:=IntToStr(i);

end;

StringGrid1.Cells[0,1]:='Шаг1';

for i:=1 to n do

begin

s:=InputBox('Введите элемент массива', '','');

a[i]:=StrToInt(s);

StringGrid1.Cells[i,1]:=IntToStr(a[i]);

end;

StringGrid2.Cells[0,0]:='min';

Button1.Enabled:=True;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

i:=0;

end;

procedure TForm1.Button1Click(Sender: TObject);

var otv:string;

begin

Button2.Enabled:=True;

Button1.Enabled:=False;

if i<n-1 then

begin

i:=i+1;

for j:=1 to n do

StringGrid2.Cells[j,1]:='';

for j:=i to n do

StringGrid2.Cells[j,1]:=StringGrid1.Cells[j,i];

min:=a[i]; nmin:=i;

StringGrid2.Cells[0,1]:=IntToStr(min);

RichEdit1.Text:='min='+IntToStr(a[i])+' позиция'+IntToStr(nmin)+Chr(13);

for j:=i to n do

begin

RichEdit1.Text:=RichEdit1.Text+IntToStr(a[j])+'<'+IntToStr(min)+'?';

if a[j]<min then

begin

min:=a[j]; nmin:=j;

RichEdit1.Text:=RichEdit1.Text+' да min='+IntToStr(a[j])+' позиция'+IntToStr(nmin)+Chr(13);

StringGrid2.Cells[0,1]:=IntToStr(min);

end

else

RichEdit1.Text:=RichEdit1.Text+' нет'+Chr(13);

end;

otv:='Минимальный элемент '+IntToStr(min)+ 'на позиции '+IntToStr(nmin);

ShowMessage(otv);

end

else

begin

ShowMessage('Массив отсортирован');

for j:=1 to n do

StringGrid2.Cells[j,1]:='';

Button1.Enabled:=false;

Button2.Enabled:=False;

end;

end;

procedure TForm1.Button2Click(Sender: TObject);

var c:integer; var j:integer; otv:string;

begin

Button1.Enabled:=True;

Button2.Enabled:=False;

otv:='Меняем местами элементы '+IntToStr(a[i])+' и '+IntToStr(a[nmin]);

ShowMessage(otv);

c:=a[i]; a[i]:=a[nmin]; a[nmin]:=c;

StringGrid1.RowCount:=n+1;

StringGrid1.Cells[0,i+1]:='Шаг'+IntToStr(i+1);

for j:=1 to n do

StringGrid1.Cells[j,i+1]:=IntToStr(a[j]);

for j:=1 to n do

StringGrid2.Cells[j,1]:='';

for j:=i+1 to n do

StringGrid2.Cells[j,1]:=IntToStr(a[j]);

end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;

Rect: TRect; State: TGridDrawState);

var q,w:Integer;

begin

StringGrid1.Canvas.Brush.Color:=clRed;

for q:=1 to n do

for w:=1 to q do

if (ACol=w) and (ARow=q) then

StringGrid1.Canvas.FrameRect(Rect);

end;

end.

Процедура, демонстрирующая сущность сортировки элементов массива простым выбором

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]