1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49:
| type TNums = Array[0..1000] of Integer; pArray = ^TNums; pPivot = ^Integer;
...
procedure TForm1.QuickSort(A: array of Integer; iLo, iHi: Integer) ; var Lo, Hi, Pivot, T: Integer; begin Lo := iLo; Hi := iHi; Pivot := A[(Lo + Hi) div 2]; repeat Lo := SortLeft(@A,@Pivot,iLo); Hi := SortLeft(@A,@Pivot,iHi); 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;
Function TForm1.SortLeft( ar : pArray; Pivot :pPivot; Pos : Integer) : Integer; var va,vb : Integer; begin va := ar[Pos]; vb := Pivot^; if va < vb then SortLeft(ar,Pivot,Pos+1) else Result:= Pos; end;
Function TForm1.SortRight(ar : pArray; Pivot :pPivot; Pos : Integer) : Integer; var va,vb : Integer; begin va := ar[Pos]; vb := Pivot^; if va > vb then SortLeft(ar,Pivot,Pos-1) else Result:= Pos; end; |