快速排序
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
Procedure QuickSort(L, R: Integer);
var
i, j, p, temp: Integer;
Begin
Repeat
i := L;
j := R;
p := (L + R) shr 1;
Repeat
while MyArray[i] < MyArray[P] do inc(i); //小于就继续往前, 一直到大于等于。
while MyArray[j] > MyArray[P] do dec(j); //大于就继续往后, 一直到小于等于。
if i <= j then //进行交换
Begin
temp := MyArray[i];
MyArray[i] := MyArray[j];
MyArray[j] := temp;
if i = p then
p := J
else if j = p then
p := i;
inc(i);
dec(j);
end;
until i > j;
if L < j then QuickSort(L, j);
L := i;
until i >= R;
end;
begin
QuickSort(1, 7);
Memo1.Lines.Clear;
for i := 1 to 7 do
Begin
Memo1.Lines.Add(IntToStr(MyArray[i]))
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i: Integer;
Procedure QuickSort(L, R: Integer);
var
i, j, p, temp: Integer;
Begin
i := L;
j := R;
p := (L + R) shr 1;
Repeat
while MyArray[i] < MyArray[P] do inc(i); //小于就继续往前, 一直到大于等于。
while MyArray[j] > MyArray[P] do dec(j); //大于就继续往后, 一直到小于等于。
if i <= j then //进行交换
Begin
temp := MyArray[i];
MyArray[i] := MyArray[j];
MyArray[j] := temp;
if i = p then
p := J
else if j = p then
p := i;
inc(i);
dec(j);
end;
until i > j;
if L < j then QuickSort(L, j);
L := i;
if i < R then QuickSort(L, R);
end;
begin
QuickSort(1, 7);
Memo1.Lines.Clear;
for i := 1 to 7 do
Begin
Memo1.Lines.Add(IntToStr(MyArray[i]))
end;
end;
procedure TStringList.SetSorted(Value: Boolean);
begin
if FSorted <> Value then
begin
if Value then Sort;
FSorted := Value;
end;
end;
function TStringList.Add(const S: string): Integer;//增加字符串,调用AddObject
begin
Result := AddObject(S, nil);
end;
function TStringList.AddObject(const S: string; AObject: TObject): Integer;
begin
if not Sorted then
Result := FCount
else
if Find(S, Result) then //查找相同的,然后根据Duplicates来判断是否添加。
case Duplicates of
dupIgnore: Exit;
dupError: Error(@SDuplicateString, 0);
end;
InsertItem(Result, S, AObject);
end;
function TStringList.IndexOf(const S: string): Integer;
begin
if not Sorted then Result := inherited IndexOf(S) else //当不排序的时候,查找到的是第一个匹配的元素。
if not Find(S, Result) then Result := -1; //当排序的时候,
end;
function TStrings.IndexOf(const S: string): Integer;
begin
for Result := 0 to GetCount - 1 do
if CompareStrings(Get(Result), S) = 0 then Exit;
Result := -1;
end;