From 3c23258256bf619eeedd251a8a62cdc3b94fe51a Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sat, 26 Sep 2020 17:52:52 +0000 Subject: [PATCH] LazStats: Add QuickSort routine. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7696 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- applications/lazstats/source/units/utils.pas | 48 +++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/applications/lazstats/source/units/utils.pas b/applications/lazstats/source/units/utils.pas index 0f0fa707e..96ec4ea2f 100644 --- a/applications/lazstats/source/units/utils.pas +++ b/applications/lazstats/source/units/utils.pas @@ -28,13 +28,15 @@ procedure Exchange(var a, b: String); overload; procedure SortOnX(X: DblDyneVec; Y: DblDyneVec = nil; Z: DblDyneVec = nil); procedure SortOnX(X: DblDyneVec; Y: DblDyneMat); +procedure QuickSortOnX(X: DblDyneVec; Y: DblDyneVec = nil; Z: DblDyneVec = nil); + function IndexOfString(L: StrDyneVec; s: String): Integer; implementation uses - ToolWin; + Math, ToolWin; // https://stackoverflow.com/questions/4093595/create-ttoolbutton-runtime procedure AddButtonToToolbar(AToolButton: TToolButton; AToolBar: TToolBar); @@ -179,6 +181,50 @@ begin end; end; +procedure QuickSortOnX(X: DblDyneVec; Y: DblDyneVec = nil; Z: DblDyneVec = nil); + + procedure DoQuickSort(L, R: Integer); + var + I,J: Integer; + P: Integer; + begin + repeat + I := L; + J := R; + P := (L+R) div 2; + repeat + while CompareValue(X[P], X[I]) > 0 do inc(I); + while CompareValue(X[P], X[J]) < 0 do dec(J); + if I <= J then begin + if I <> J then begin + Exchange(X[I], X[J]); + if Y <> nil then + Exchange(Y[I], Y[J]); + if Z <> nil then + Exchange(Z[I], Z[J]); + end; + + if P = I then + P := J + else if P = J then + P := I; + + inc(I); + dec(J); + end; + until I > J; + + if L < J then + DoQuickSort(L, J); + + L := I; + until I >= R; + end; + +begin + DoQuickSort(0, High(X)); +end; + function IndexOfString(L: StrDyneVec; s: String): Integer; var i: Integer;