@@ -766,59 +766,94 @@ function LoadFile(filename : String):TBytes;
766766 End ;
767767 TIndexArray = array of TIndex;
768768
769+
769770Procedure QuickSortIndex (var a : TIndexArray);
770771
772+ Function MedianOfThree (L, M, R: Integer): Integer;
773+ Begin
774+ If a[L].id <= a[M].id Then
775+ Begin
776+ If a[M].id <= a[R].id Then
777+ Result := M // L <= M <= R
778+ Else If a[L].id <= a[R].id Then
779+ Result := R // L <= R < M
780+ Else
781+ Result := L // R < L <= M
782+ End
783+ Else
784+ Begin
785+ If a[L].id <= a[R].id Then
786+ Result := L // M < L <= R
787+ Else If a[M].id <= a[R].id Then
788+ Result := R // M <= R < L
789+ Else
790+ Result := M // R < M < L
791+ End ;
792+ End ;
793+
771794 Procedure QuickSort (L, R: Integer);
772795 Var
773796 I, J, K : Integer;
774797 t : TIndex;
775798 Begin
776- // QuickSort routine (Recursive)
777- // * Items is the default indexed property that returns a pointer, subclasses
778- // specify these return values as their default type.
779- // * The Compare routine used must be aware of what this pointer actually means.
780-
781- Repeat
799+ While L < R Do
800+ Begin
782801 I := L;
783802 J := R;
784- K := (L + R) Shr 1 ;
803+
804+ // BETTER PIVOT SELECTION - median of three
805+ K := MedianOfThree(L, (L + R) Shr 1 , R);
806+
807+ // Move pivot to middle position for consistency
808+ If K <> ((L + R) Shr 1 ) Then
809+ Begin
810+ t := a[K];
811+ a[K] := a[(L + R) Shr 1 ];
812+ a[(L + R) Shr 1 ] := t;
813+ K := (L + R) Shr 1 ;
814+ End ;
785815
786816 Repeat
787817 While a[I].id < a[K].id Do
788818 Inc(I);
789-
790819 While a[J].id > a[K].id Do
791820 Dec(J);
792-
793821 If I <= J Then
794822 Begin
795823 t := a[i];
796824 a[i] := a[j];
797825 a[j] := t;
798-
799- // Keep K as the index of the original middle element as it might get exchanged.
800826 If I = K Then
801827 K := J
802828 Else If J = K Then
803829 K := I;
804-
805830 Inc(I);
806831 Dec(J);
807832 End ;
808833 Until I > J;
809834
810- If L < J Then
811- QuickSort(L, J);
812-
813- L := I;
814- Until I >= R;
835+ // Recurse on smaller partition
836+ If (J - L) < (R - I) Then
837+ Begin
838+ If L < J Then
839+ QuickSort(L, J);
840+ L := I;
841+ End
842+ Else
843+ Begin
844+ If I < R Then
845+ QuickSort(I, R);
846+ R := J;
847+ End ;
848+ End ;
815849 End ;
816850
817851Begin
818852 If length(a) > 1 Then
819853 QuickSort(0 , length(a) - 1 );
820854End ;
821855
856+
822857procedure TSnomedImporter.ReadDescriptionsFile ;
823858var
824859 s : TBytes;
0 commit comments