Пузырьковый метов обработки (Delphi)

Алгоритмы: от сортировки пузырьком до численных методов

Модераторы: C_O_D_E, DeeJayC

Ответить
NightSun
Сообщения: 0
Зарегистрирован: 24 фев 2014, 20:16

24 фев 2014, 20:25

Здравствуйте! Имеется недоделанная программа которая сортирует значения пузырьковым методом в StringGrid.
http://rghost.ru/52641190
Вот код

Код: Выделить всё

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
   b:string;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[0,0]:='Номер элемента';
StringGrid1.Cells[1,0]:='1';
StringGrid1.Cells[2,0]:='2';
StringGrid1.Cells[3,0]:='3';
StringGrid1.Cells[4,0]:='4';
StringGrid1.Cells[5,0]:='5';


StringGrid1.Cells[0,1]:='Исходный массив';
StringGrid1.Cells[0,2]:='Первый просмотр';
StringGrid1.Cells[0,3]:='Второй просмотр';
StringGrid1.Cells[0,4]:='Третий просмотр';
StringGrid1.Cells[0,5]:='Четвертый просмотр';

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 if strtoint(stringgrid1.Cells[1,2])> strtoint(stringgrid1.Cells[2,2])
 then
 begin b:=stringgrid1.Cells[2,2];
        stringgrid1.Cells[2,2]:=stringgrid1.Cells[1,2];
        stringgrid1.Cells[1,2]:=b;
end;

 if strtoint(stringgrid1.Cells[2,2])> strtoint(stringgrid1.Cells[3,2])
 then
 begin b:=stringgrid1.Cells[3,2];
        stringgrid1.Cells[3,2]:=stringgrid1.Cells[2,2];
        stringgrid1.Cells[2,2]:=b;
  end;

if strtoint(stringgrid1.Cells[3,2])> strtoint(stringgrid1.Cells[4,2])
 then
 begin b:=stringgrid1.Cells[4,2];
        stringgrid1.Cells[4,2]:=stringgrid1.Cells[3,2];
        stringgrid1.Cells[3,2]:=b;
  end;
end;
Этот код работает немного не правильно, он сортирует значения в столбцах, но сортирует значения там же где они и написаны, то есть нужно сделать, чтобы исходные значения оставались на своём месте, а новые(отсортированные) выводились на след. строку

Изображение
Ответить