procedure TForm1.FormCreate(Sender: TObject);
procedure QuickSort(var A: array of Integer; iLo, iHi: Integer);
var
Lo, Hi, Mid, T: Integer;
begin
Lo := iLo;
Hi := iHi;
Mid := A[(Lo + Hi) div 2];
repeat
while A[Lo] < Mid do Inc(Lo);
while A[Hi] > Mid do Dec(Hi);
if Lo <= Hi then begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSort(A, iLo, Hi);
if Lo < iHi then QuickSort(A, Lo, iHi);
end;
const
DataCount = 10000;
var
DataList: array[1..DataCount] of Integer;
I, J: Integer;
B: Boolean;
T: DWORD;
begin
with TStringList.Create do try
///////Begin 提供模拟数据
Randomize;
for I := 1 to DataCount do begin
DataList[I] := Random(DataCount);
Add(IntToStr(DataList[I]));
end;
SaveToFile('C:\Calc0.txt');
Clear;
///////End 提供模拟数据
T := GetTickCount;
QuickSort(DataList, Low(DataList), High(DataList));
J := -1;
B := True;
for I := 1 to DataCount do
if J = DataList[I] then
if B then begin
Add(IntToStr(DataList[I]));
B := False;
end else
else begin
B := True;
J := DataList[I];
end;
SaveToFile('C:\Calc1.txt');
finally
Free;
end;
Caption := Format('%d', [GetTickCount - T]); //我的计算机是39毫秒
end;
procedure QuickSort(var A: array of Integer; iLo, iHi: Integer);
var
Lo, Hi, Mid, T: Integer;
begin
Lo := iLo;
Hi := iHi;
Mid := A[(Lo + Hi) div 2];
repeat
while A[Lo] < Mid do Inc(Lo);
while A[Hi] > Mid do Dec(Hi);
if Lo <= Hi then begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSort(A, iLo, Hi);
if Lo < iHi then QuickSort(A, Lo, iHi);
end;
const
DataCount = 10000;
var
DataList: array[1..DataCount] of Integer;
I, J: Integer;
B: Boolean;
T: DWORD;
begin
with TStringList.Create do try
///////Begin 提供模拟数据
Randomize;
for I := 1 to DataCount do begin
DataList[I] := Random(DataCount);
Add(IntToStr(DataList[I]));
end;
SaveToFile('C:\Calc0.txt');
Clear;
///////End 提供模拟数据
T := GetTickCount;
QuickSort(DataList, Low(DataList), High(DataList));
J := -1;
B := True;
for I := 1 to DataCount do
if J = DataList[I] then
if B then begin
Add(IntToStr(DataList[I]));
B := False;
end else
else begin
B := True;
J := DataList[I];
end;
SaveToFile('C:\Calc1.txt');
finally
Free;
end;
Caption := Format('%d', [GetTickCount - T]); //我的计算机是39毫秒
end;
解决方案 »
- delphi 配置文件工具一点小问题请教师傅们
- 帮帮忙
- 请问List index out of bounds(0)错误是怎么回事?
- Delphi5里还有NMmsg控件的,D7就没有了,怎么办啊?????????????????
- 如何在可执行程序里调用web上的搜索引擎并把结果返回??
- 急!!我想查找字段(散客)的值为是的所有记录,出错!
- 获得了一个TStream对象,怎么利用它来生成一个文件?
- 我已经把*.SWF文件和程序连接起来了,如何实现当打开*.swf时调用我的文件来播放它?(双击*.SWF文件)
- 大家好,向大家提个问题“如何动态控制控件的显示”分只能加这么多,以后再补,请进来看看!
- 我的Edit一输入就闪退(XE7),什么原因
- 请问到哪里去找tdxdbgrid控件?
- :我想在WIN2000下,写一个ISAPI程序,目的是使用WORD的COM接口将DOC文件另存为TXT.可是COM在ISAPI中无法使用.请赐教
都是循环10000次
共循环20000次,
我用你的方法看时间就为0,
我加了一个 for i:=0 to 1000000 时间为10
time1:=gettickcount;
计算、查找;
time2:=gettickcount;time2-time1就是所用的望穿毫秒数。
procedure TForm1.Button1Click(Sender: TObject);
var
dd:array[1..10000] of integer;
dd1:array[1..10000] of integer;
dd2:array[1..10000] of integer;
i,j:integer;
b:Dword;
begin
randomize;
j:=0;
for i:=1 to 10000 do //初始化
begin
dd[i]:=random(19)+1;
dd1[i]:=0;
dd2[i]:=0;
end;
b:= GetTickCount;
for i:=1 to 10000 do
begin
if dd1[dd[i]]=0 then
dd1[dd[i]]:=dd[i]
else
begin
if dd2[dd[i]]=0 then
begin
j:=j+1;
dd2[dd[i]]:=dd[i];
end;
end;
end;
form1.Caption:=Format('%d', [GetTickCount - b]);
end;
而且你这里的j做什么用的
8
10
15
15
4
14
13
15
1
15
17
12
18
1
10
9
12
4
18
8
8
1
10
15
7
2
9
15
10
5
2
4
12
3
6
8
15
17
2
12
10
16
19
16
8
12
11
14
1
19
10
14
7
10
19
2
9
15
2
2
1
8
10
8
14
3
16
8
13
2
13
13
16
9
17
2
3
1
14
15
15
1
16
18
14
9
6
14
7
5
11
5
6
4
19
5
19
16
2
16
///////Memo2.Text???????????????????????????????
4
14
15
17
18
19
20
27
31
38
44
48
50
53
66
69
87
90
91
////////////////////////////////////
procedure TForm1.FormCreate(Sender: TObject);
const
cCount = 100;
var
dd: array[1..cCount] of Integer;
dd1: array[1..cCount] of Integer;
dd2: array[1..cCount] of Integer;
I: Integer;
J: Integer;
B: DWORD;
begin
Randomize;
with TStringList.Create do try
for I := 1 to cCount do
begin
dd[I] := Random(19) + 1;
Add(IntToStr(dd[I]));
dd1[I] := 0;
dd2[I] := 0;
end;
Memo1.Text := Text;
Clear;
J := 0; //没有用
B := GetTickCount;
for I := 1 to cCount do
begin
if dd1[dd[I]] = 0 then
dd1[dd[I]] := dd[I]
else
begin
if dd2[dd[I]]=0 then
begin
j:=j+1; //没有用
dd2[dd[I]] := dd[I];
Add(IntToStr(I));
end;
end;
end;
Memo2.Text := Text;
Caption := Format('Time:%d,J:%d', [GetTickCount - B, J]);
finally
Free;
end;
end;
// ~~~~~~抱歉这里写成I了