기술자료
소트 알고리즘.
theF
2009. 12. 8. 17:38
몇가지 소트 알고리즘
procedure BubbleSort(Items: TStrings);
var
done: boolean;
i, n: integer;
Dummy: string;
begin
n := Items.Count;
repeat
done := true;
for i := 0 to n - 2 do
if Items[i] > Items[i + 1] then
begin
Dummy := Items[i];
Items[i] := Items[i + 1];
Items[i + 1] := Dummy;
done := false;
end;
until done;
end;
procedure SelectionSort(Items: TStrings);
var
i, n, maxIndex, topIndex: integer;
Dummy: string;
begin
n := Items.Count;
for topIndex := n - 1 downto 1 do
begin
maxIndex := topIndex;
for i := 0 to topIndex - 1 do
if Items[i] > Items[maxIndex] then
maxIndex := i;
Dummy := Items[topIndex];
Items[topIndex] := Items[maxIndex];
Items[maxIndex] := Dummy;
end;
end;
procedure InsertionSort(Items: TStrings);
var
i, Position, n: integer;
Value: string;
Done : boolean;
begin
n := Items.Count;
for i := 1 to n - 1 do
begin
Value := Items[i];
Position := i;
Done := false;
while not done do
begin
if Position <= 0 then
Done := true
else
if Value >= Items[Position - 1] then
Done := true
else
begin
Items[Position] := Items[Position - 1];
Position := Position - 1;
end;
end;
Items[Position] := Value;
end;
end;