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;

解决方案 »

  1.   

    我的程序没有排序,没有查找,只有2个for循环
    都是循环10000次
    共循环20000次,
    我用你的方法看时间就为0,
     我加了一个 for i:=0 to 1000000 时间为10
      

  2.   

    To zsy_good(路漫漫其修远兮,吾将上下而求索)计算时间?
    time1:=gettickcount;
    计算、查找;
    time2:=gettickcount;time2-time1就是所用的望穿毫秒数。
      

  3.   

    //看看行不行
    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;
      

  4.   

    虽然时间上很快,你考虑了如果数组里的数很小,dd2和dd1都会浪费很多空间的,
    而且你这里的j做什么用的
      

  5.   

    我也 同意加入hash表,不要开太多的数组空间
      

  6.   

    ///////Memo1.Text???????????????????
    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;
      

  7.   

              Add(IntToStr(dd[I]));
    //                     ~~~~~~抱歉这里写成I了