-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtpl_algo_quicksort.inc
More file actions
292 lines (259 loc) · 10.2 KB
/
Copy pathtpl_algo_quicksort.inc
File metadata and controls
292 lines (259 loc) · 10.2 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
(*
(C) 2006-2014 George Bakhtadze.
Generic quicksort algorithm implementation
+ optimized generic quicksort implementation
+ constant memory consumption (64 * 2 * SizeOf(Integer) bytes)
+ custom inlineable comparator or evaluator functions support
+ indirect sorting with additional index support, without data move
+ any indexable with "[]" collection support - raw arrays, template vectors, etc
+ resistive to quicksort-unfriendly data (soBadData option)
+ no recursion
- doesn't preserve relative order of equal values
Usage:
--------------------------
procedure/function <Name>(_SortCount: Integer; _SortData: <any array of _SortDataType> [;_SortIndex: <any array of integer> ]);
type
_SortDataType = <Your type>;
[_SortValueType = <any comparable with "<" type>]
const _SortOptions = "["soBadData, soDescending"]";
[<_SortGetValue() or _SortCompare() function>]
{$I tpl_algo_quicksort.inc}
end;
--------------------------
include soDescending in _SortOptions constant to perform sort in descending order
include soBadData in _SortOptions constant to use a random index for base element. This can be ~5% slower on usual data but good for quicksort-unfriendly data.
if _SortDataType is a type which can not be compared with "<" operation one of the following functions should be declared:
function _SortCompare(const V1,V2: _SortDataType): <Some numeric type>;
begin
Result := V1 - V2; // As usual
end;
or
function _SortGetValue(const V: _SortDataType): _SortValueType;
begin
Result := <some numeric expression>;
end;
if _SortCompare() function is declared the algorithm will call it each time when a comparation of two data values is needed
if _SortGetValue() function is declared the algorithm will call it each time when a comparable representation of data value is needed
--------------------------
Examples:
// Example 1: Sort array of integers in accending order
procedure Sort(const _SortCount: Integer; var _SortData: Array of Integer);
type _SortDataType = Integer;
{$MESSAGE 'Instantiating sort algorithm <Integer>'}
{$I tpl_algo_quicksort.inc}
end;
// Example 2: Sort template vector of complex numbers by module in descending order
procedure Sort(const _SortCount: Integer; _SortData: TComplexVector);
const _SortOptions = [soDescending];
type _SortDataType = TComplex;
function _SortCompare(const V1, V2: _SortDataType): Integer; {$I inline.inc}
begin
Result := (TComplex.GetModule(V1) - TComplex.GetModule(V2));
end;
{$MESSAGE 'Instantiating sort algorithm <TComplex>'}
{$I tpl_algo_quicksort.inc}
end;
*)
{$IF Declared(_SortIndex)}
{$DEFINE _SORTINDEXED}
{$IFEND}
{$IF Declared(_SortCompare)}
{$IFNDEF _SORTNOCOMPAREFUNC}
{$DEFINE _SORTCOMPAREFUNC}
{$ENDIF}
{$ELSE}
{$UNDEF _SORTCOMPAREFUNC}
{$IFEND}
{$IF Declared(_SortGetValue)}
{$IFNDEF _SORTNOCOMPUTEFUNC}
{$DEFINE _SORTCOMPUTEFUNC}
{$ENDIF}
{$ELSE}
{$UNDEF _SORTCOMPUTEFUNC}
{$IFEND}
{$IF Declared(_SortOptions)}
{$I tpl_algo_sort_opt.inc}
{$IFEND}
{$IF not Declared(_SortValueType)}
type _SortValueType = Integer;
{$IFEND}
{$IF not Declared(_SORT_INSERTION_THRESHOLD)}
// if number of elements <= this value, insertion sort will be used
const _SORT_INSERTION_THRESHOLD = 48;
{$IFEND}
const
{$IFDEF CPU64}
StackSize = 64; // Enough for 2^64 sort data size
{$ELSE}
StackSize = 32; // Enough for 2^32 sort data size
{$ENDIF}
var
Stack: array[0..StackSize-1] of record
l, r: Integer;
end;
n1, n2, L, R: Integer;
{$IFDEF _SORTINDEXED}ti: Integer;{$ENDIF}
bv, t: _SortDataType;
StackPTR: Integer;
{$IFDEF _SORTBADDATA}
Seed: Cardinal;
temp: Integer;
{$ENDIF}
{$IFDEF _SORTCOMPUTEFUNC}
TempValue: _SortValueType;
{$ENDIF}
begin
{$IFDEF _SORTBADDATA}
Seed := 1000;
{$ENDIF}
StackPTR := 0;
Stack[0].l := 0;
Stack[0].r := _SortCount-1;
repeat
L := Stack[StackPTR].l;
R := Stack[StackPTR].r;
Dec(StackPTR);
// Insertion sort
if R - L < _SORT_INSERTION_THRESHOLD then begin
for n1 := L+1 to R do begin
n2 := n1-1;
{$IFDEF _SORTINDEXED}
ti := _SortIndex[n1];
bv := _SortData[ti];
{$ELSE}
bv := _SortData[n1];
{$ENDIF}
while (n2 >= L) do begin
{$IFNDEF _SORTDESCENDING}
{$IFDEF _SORTCOMPUTEFUNC}
if not (_SortGetValue(bv) < _SortGetValue(_SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}])) then Break;
{$ELSE}
{$IFDEF _SORTCOMPAREFUNC}
if not (_SortCompare(bv, _SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}]) < 0) then Break;
{$ELSE}
if not (bv < _SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}]) then Break;
{$ENDIF}
{$ENDIF}
{$ELSE}
{$IFDEF _SORTCOMPUTEFUNC}
if not (_SortGetValue(_SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}]) < _SortGetValue(bv)) then Break;
{$ELSE}
{$IFDEF _SORTCOMPAREFUNC}
if not (_SortCompare(_SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}], bv) < 0) then Break;
{$ELSE}
if not (_SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}] < bv) then Break;
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$IFDEF _SORTINDEXED}
_SortIndex[n2+1] := _SortIndex[n2];
{$ELSE}
_SortData[n2+1] := _SortData[n2];
{$ENDIF}
(* _SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2+1]{$ELSE}n2+1{$ENDIF}] := _SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}];*)
Dec(n2);
end;
// Move(_SortData[n2+1], _SortData[n2+2], SizeOf(_SortDataType) * (n1-n2-1));
if n1 <> n2+1 then
{$IFDEF _SORTINDEXED}
_SortIndex[n2+1] := ti;
{$ELSE}
_SortData[n2+1] := bv;
{$ENDIF}
end;
end else while (L < R) do begin
// Get separating element
{$IFDEF _SORTBADDATA} {$Q-}
Seed := 97781173 * Seed + 7;
temp := R - L - 1;
temp := temp or temp shr 1;
temp := temp or temp shr 2;
temp := temp or temp shr 4;
temp := temp or temp shr 8;
temp := temp or temp shr 16;
{$IFDEF CPU64}
temp := temp or temp shr 32;
{$ENDIF}
bv := _SortData[{$IFDEF _SORTINDEXED}_SortIndex[Cardinal(L) + Seed and (temp shr 1)]{$ELSE}Cardinal(L) + Seed and (temp shr 1){$ENDIF}];
{$ELSE}
bv := _SortData[{$IFDEF _SORTINDEXED}_SortIndex[(L+R) div 2]{$ELSE}(L+R) div 2{$ENDIF}];
{$ENDIF}
{$IFDEF _SORTCOMPUTEFUNC}
TempValue := _SortGetValue(bv);
{$ENDIF}
n1 := L;
n2 := R;
while n1 <= n2 do begin
{$IFNDEF _SORTDESCENDING}
{$IFDEF _SORTCOMPUTEFUNC}
while True do if _SortGetValue(_SortData[{$IFDEF _SORTINDEXED}_SortIndex[n1]{$ELSE}n1{$ENDIF}]) < TempValue then Inc(n1) else Break; // While conditions not inlined by some stupid reason
while True do if TempValue < _SortGetValue(_SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}]) then Dec(n2) else Break;
{$ELSE}
{$IFDEF _SORTCOMPAREFUNC}
while True do if _SortCompare(_SortData[{$IFDEF _SORTINDEXED}_SortIndex[n1]{$ELSE}n1{$ENDIF}], bv) < 0 then Inc(n1) else Break;
while True do if _SortCompare(bv, _SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}]) < 0 then Dec(n2) else Break;
{$ELSE}
while _SortData[{$IFDEF _SORTINDEXED}_SortIndex[n1]{$ELSE}n1{$ENDIF}] < bv do Inc(n1);
while bv < _SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}] do Dec(n2);
// while True do if _SortData[n1] < bv then Inc(n1) else Break;
// while True do if bv < _SortData[n2] then Dec(n2) else Break;
{$ENDIF}
{$ENDIF}
{$ELSE}
{$IFDEF _SORTCOMPUTEFUNC}
while True do if TempValue < _SortGetValue(_SortData[{$IFDEF _SORTINDEXED}_SortIndex[n1]{$ELSE}n1{$ENDIF}]) then Inc(n1) else Break;
while True do if _SortGetValue(_SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}]) < TempValue then Dec(n2) else Break;
{$ELSE}
{$IFDEF _SORTCOMPAREFUNC}
while True do if _SortCompare(bv, _SortData[{$IFDEF _SORTINDEXED}_SortIndex[n1]{$ELSE}n1{$ENDIF}]) < 0 then Inc(n1) else Break;
while True do if _SortCompare(_SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}], bv) < 0 then Dec(n2) else Break;
{$ELSE}
while bv < _SortData[{$IFDEF _SORTINDEXED}_SortIndex[n1]{$ELSE}n1{$ENDIF}] do Inc(n1);
while _SortData[{$IFDEF _SORTINDEXED}_SortIndex[n2]{$ELSE}n2{$ENDIF}] < bv do Dec(n2);
// while True do if bv < _SortData[n1] then Inc(n1) else Break;
// while True do if _SortData[n2] < bv then Dec(n2) else Break;
{$ENDIF}
{$ENDIF}
{$ENDIF}
if n1 <= n2 then
begin
{$IFDEF _SORTINDEXED}
ti := _SortIndex[n1];
_SortIndex[n1] := _SortIndex[n2];
_SortIndex[n2] := ti;
{$ELSE}
t := _SortData[n1];
_SortData[n1] := _SortData[n2];
_SortData[n2] := t;
{$ENDIF}
Inc(n1);
Dec(n2);
end;
end;
// { _SortData[si..n1<bi] < _SortData[bi] AND _SortData[bi<n2..fi] > _SortData[bi] }
if (n2-L) > (R-n1) then
begin
if L < n2 then
begin
Inc(StackPTR);
Stack[StackPTR].l := L;
Stack[StackPTR].r := n2;
end;
L := n1;
end else begin
if n1 < R then
begin
Inc(StackPTR);
Stack[StackPTR].l := n1;
Stack[StackPTR].r := R;
end;
R := n2;
end;
end;
until StackPTR < 0;
{$UNDEF _SORTCOMPAREFUNC}
{$UNDEF _SORTCOMPUTEFUNC}
{$UNDEF _SORTDESCENDING}
{$UNDEF _SORTBADDATA}
{$UNDEF _SORTNOCOMPAREFUNC}
{$UNDEF _SORTNOCOMPUTEFUNC}