- Code: Select all
procedure Sort(const AIn: array of Integer; var AOut: Array of Integer);
var
I, J, K, Found: Integer;
begin
for I:=0 to High(AIn) do
AOut[I]:=-1;
AOut[0]:=AIn[0];
for I:=1 to High(AIn) do
begin
Found:=AIn[I];
for J:=0 to High(AIn) do
begin
if AOut[J]>Found then
begin
for K:=High(AIn) downto J-1 do
begin
if K-1>=0 then
AOut[K]:=AOut[K-1]
end;
AOut[J]:=Found;
Break;
end
else
if AOut[J]=-1 then
AOut[J]:=Found;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Size, I: Integer;
Unsorted, Sorted: array of Integer;
Contains: string;
begin
Randomize;
Size:=StrToInt(Edit1.Text);
SetLength(Unsorted, Size);
SetLength(Sorted, Size);
Memo1.Lines.Add('Log: Begin');
for I:=0 to High(Unsorted) do
begin
Unsorted[I]:=Random(255);
Contains:=Contains+IntToStr(Unsorted[I])+', ';
end;
Memo1.Lines.Add(Contains);
Memo1.Lines.Add('Log: Begin sort');
Contains:='';
Sort(Unsorted, Sorted);
Memo1.Lines.Add('Log: End sort');
for I:=0 to High(Sorted) do
begin
Contains:=Contains+IntToStr(Sorted[I])+', ';
end;
Memo1.Lines.Add(Contains);
Memo1.Lines.Add('Log: End');
end;
Code compiles, program nicely executes, but sorting is simply bad... for example:
in AIn table are numbers: 88, 57, 214, 228, 83, 72, 250, 16, 205, 9
in AOut table, after sort are numbers: 9, 16, 57, 72, 83, 83, 205, 214, 214, 214
sort is working (as you can see items are sorted in model "a<b then a,b") but why bolded elements are doubled?