Сортировка 2-х мерного массива
Уважаемые помогите
Нужен код сортировки двоичного включения,прямого обмена и быстрой сортировки для 2-х мерного массива)) unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Grids, DBGrids; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Label2: TLabel; StringGrid1: TStringGrid; Button3: TButton; Button4: TButton; Label3: TLabel; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); const n=100; b=100; var s,c,i,j:integer; Mas: Array[1..n , 1..b] of Integer; begin s:=strtoint(edit1.text); c:=strtoint(edit2.text); StringGrid1.RowCount:=s; StringGrid1.ColCount:=c; Randomize; for i:=0 to s-1 do for j:=0 to c-1 do begin StringGrid1.Cells[i,j]:=IntToStr(Random(100)-40); end; end; procedure TForm1.Button3Click(Sender: TObject); const n=100; b=100; var a,q,c,d:integer;st:string; Mas: Array[1..n , 1..b] of Integer; begin a:=strtoint(edit1.text); q:=strtoint(edit2.text); StringGrid1.RowCount:=a; StringGrid1.ColCount:=q; for c:=0 to a do for d:=0 to q do end; procedure TForm1.Button4Click(Sender: TObject); var n:integer; begin for n:=StringGrid1.Rowcount downto 0 do begin StringGrid1.Rows[n].clear; if (StringGrid1.RowCount=0) then begin StringGrid1.Rows[0].clear; exit; end; StringGrid1.Rowcount:=StringGrid1.Rowcount-1; end; end; Здесь я пытаюсь сделать прямой обмен но извилин к сожелению не хватает чтобы переделать в 2-х мерный массив(( procedure TForm1.Button2Click(Sender: TObject); const SIZE=100; var a:array[1..SIZE] of integer; k:integer; // текущий элемент массива i:integer; // индекс для ввода и вывода массива changed:boolean; // TRUE, если в текущем цикле были обмены buf:integer; // буфер для обмена элементов массива begin // ввод массива for i:=1 to SIZE do a[i] := StrToInt(StringGrid1.Cells[i-1,0]); label3.caption:=''; // сортирвка массива repeat changed:=FALSE; // пусть в текущем цикле нет обменов for k:=0 to SIZE-1 do if a[k] > a[k+1] then begin // обменяем k-ый и k+1-ый элементы buf := a[k]; a[k] := a[k+1]; a[k+1] := buf; changed := TRUE; end; // вывод массива for i:=0 to SIZE do Label3.caption:=label3.caption+' '+IntTostr(a[i]); Label3.caption:=label3.caption+#13; until not changed; // если не было обменов, значит // массив отсортирован Label3.caption:=label3.caption+#13+'Массив отсортирован.'; end; end. |
Часовой пояс GMT +4, время: 23:43. |
Powered by vBulletin® Version 4.5.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.