달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

소트 알고리즘.

기술자료 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;
Posted by theF
|