How can I implement a quick sort in Delphi without getting Access violation errors for large numbers of records?

隐身守侯 提交于 2019-12-04 07:11:10

问题


Here is my current code:

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;

How can this be stabilized, ideally changing as little code as possible. IS it a problem with using dynamic arrays? I need to be able to sort through at least 600 records without error.


回答1:


Your code cannot be salvaged. You are going about solving this problem in the wrong way and I advise you to abandon your existing code. Here is how I believe sorting should be done.

Note that I am assuming that you don't have generics available to you. In modern Delphi versions you can use TArray.Sort<T> from Generics.Collections to sort. If you have access to that, you should use it

First of all the key is to separate the sorting from the array being sorted. To achieve that define the following types:

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

The point is that all the common algorithms that can sort an array need only to be able to compare two items, and exchange two items. These procedural types enable separation of the sorting algorithm from the underlying array storage and types.

With these definitions in place, we are ready to write our general purpose sorting algorithms. For quicksort it looks like this:

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;

In order to use this you need to wrap your array in a class which exposes compare and exchange methods.



来源:https://stackoverflow.com/questions/21699696/how-can-i-implement-a-quick-sort-in-delphi-without-getting-access-violation-erro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!