Procedure QuickSort(FirstIndex, LastIndex: Integer; Function InSequence(Low, High: Integer); Procedure Swap(A, B: Integer) ); { QuickSort sorts an arbitrary sequence of objects. The objects are indexed from FirstIndex up to LastIndex. Function InSequence whether the object with index Low is considered to be strictly less than the object with index High. Procedure Swap interchanges the objects having indices A and B in the sequence. Restrictions: abs(FirstIndex) <= maxint div 2; abs(LastIndex) <= maxint div 2; and LastIndex - FirstIndex <= maxint } Procedure Sort(Min, Max: Integer); Var Lo, Hi, Split: Integer; Begin { Sort } Repeat { Min < Max } If InSequence(Max,Min) then Swap(Max,Min); If Max - Min > 1 then Begin { Locate median of three objects } Split := (Min + Max) div 2; If InSequence(Split,Min) then Swap(Min,Split); If InSequence(Max,Split) then Swap(Max,Split); If Max - Min > 2 then { more than three objects to sort } Begin Lo := Min; Hi := Max; Repeat { partition section } While InSequence(Lo,Split) do Lo := Lo + 1; While InSequence(Split,Hi) do Hi := Hi - 1; If Lo <= Hi then Begin If Lo < Hi then Swap(Lo,Hi); Lo := Lo + 1; Hi := Hi - 1 End Until Lo > Hi; If Hi - Min < Max - Lo then Begin If Min < Hi then Sort(Min,Hi); Min := Lo End else Begin If Lo < Max then Sort(Lo,Max); Max := Hi End End else Min := Max { section got sorted in median test } End else Min := Max { section got sorted already } Until Min >= Max End { sort } ; Begin { QuickSort } If FirstIndex < LastIndex then Sort(FirstIndex,LastIndex) End { QuickSort } ;