2014-02-11 18 views
-3

這裏是我當前的代碼:如何在Delphi中實現快速排序而不會導致大量記錄的訪問衝突錯誤?

function StudentQuickSort(StudentList:TStudentArray;ArrayLength:integer):TStudentArray; 
var 
    Pivot:TstudentArray; 
    LesserList:TStudentArray; 
    GreaterList:TstudentArray; 
    ArrayCount:Integer; 
    LesserCount:Integer; 
    GreaterCOunt:integer; 
procedure ConcatArrays(const A,B,C: TStudentArray; var D: TStudentArray); 
    var i, nA,nB,nC: integer; 
    begin 
    nA := length(A); 
    nB := length(B); 
    nC := Length(C); 
    SetLength(D,nA+nB+nC); 
    for i := 0 to nA-1 do 
     D[i] := A[i]; 
    for i := 0 to nB-1 do 
     D[i+nA] := B[i]; 
    for i := 0 to nC-1 do 
     D[i+nA+nB] := C[i]; 
    end; 

begin 
    if Arraylength<=1 then 
    begin 
     Result:=(StudentList); 
    end 
    else 
    begin 
     SetLength(StudentList,ArrayLength); 
     SetLength(LesserList,ArrayLength); 
     SetLength(GreaterList,ArrayLength); 
     SetLength(Pivot,1); 
     LesserCOunt:=0; 
     GreaterCount:=0; 
     Pivot[0]:=StudentList[0]; 
     for ArrayCount := 1 to ArrayLength-1 do 
     begin 
      if strtoint(StudentList[ArrayCount].StudentNo)>strtoint(Pivot[0].StudentNo) then 
      begin 
       GreaterList[GreaterCOunt]:=StudentList[ArrayCount]; 
       GreaterCount:=GreaterCount+1; 
      end 
     else 
      begin 
       LesserList[LesserCOunt]:=StudentList[ArrayCount]; 
       LesserCount:=LesserCount+1; 
      end; 
     end; 
     SetLength(LesserLIst,LesserCount); 
     SetLength(GreaterList,GreaterCount); 
     ConcatArrays(StudentQuickSort(LesserList,LesserCount),Pivot,StudentQuickSort(GreaterList,GreaterCount),Result) 
    end; 
end; 

這怎麼可能穩定,理想的改變,因爲少的代碼越好。這是使用動態數組的問題嗎?我需要能夠無錯地排序至少600條記錄。

+1

嘗試使用調試器來查看您獲取AV的哪一行? – whosrdaddy

+0

數組大小不是問題。問題是你的代碼存在缺陷。如果我是你,我會使用現有的排序代碼。 –

+0

我寧願修復我的代碼,而不是使用預先存在的排序功能。代碼中有哪些缺陷? – rowanphilip

回答

3

您的代碼無法打撈。你正在以錯誤的方式解決這個問題,我建議你放棄現有的代碼。以下是我相信排序應該完成的方式。

請注意,我假設您沒有可用的泛型。在現代Delphi版本中,您可以使用TArray.Sort<T>Generics.Collections進行排序。如果你有權訪問它,你應該使用它

首先,關鍵是將排序與正在排序的數組分開。爲了實現這一定義下列類型:

type 
    TCompareIndicesFunction = function(Index1, Index2: Integer): Integer of object; 
    TExchangeIndicesProcedure = procedure(Index1, Index2: Integer) of object; 

的一點是,所有能數組排序常見的算法需要只能夠比較兩個項目,並交換兩個項目。這些過程類型可以將排序算法從底層數組存儲和類型中分離出來。使用這些定義,我們準備編寫我們的通用排序算法。對於快速排序,它看起來是這樣的:

procedure QuickSort(Count: Integer; Compare: TCompareIndicesFunction; 
    Exchange: TExchangeIndicesProcedure); 

    procedure Sort(L, R: Integer); 
    var 
    I, J, P: Integer; 
    begin 
    repeat 
     I := L; 
     J := R; 
     P := (L+R) div 2; 
     repeat 
     while Compare(I, P)<0 do inc(I); 
     while Compare(J, P)>0 do dec(J); 
     if I<=J then 
     begin 
      if I<>J then 
      begin 
      Exchange(I, J); 
      //may have moved the pivot so we must remember which element it is 
      if P=I then 
       P := J 
      else if P=J then 
       P := I; 
      end; 
      inc(I); 
      dec(J); 
     end; 
     until I>J; 
     if L<J then 
     Sort(L, J); 
     L := I; 
    until I>=R; 
    end; 

begin 
    if Count>0 then 
    Sort(0, Count-1); 
end; 

爲了使用這個你需要用你的數組中暴露比較和交換方法的類。