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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
{
This file is part of the Free Pascal Run Time Library (rtl)
Copyright (c) 1999-2019 by the Free Pascal development team
This file provides the base for the pluggable sorting algorithm
support. It also provides a default QuickSort implementation.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit sortbase;
{$MODE objfpc}
interface
type
TListSortComparer_NoContext = function(Item1, Item2: Pointer): Integer;
TPtrListSorter_NoContext = procedure(ItemPtrs: PPointer; ItemCount: SizeUInt; Comparer: TListSortComparer_NoContext);
TItemListSorter_NoContext = procedure(Items: Pointer; ItemCount, ItemSize: SizeUInt; Comparer: TListSortComparer_NoContext);
TListSortComparer_Context = function(Item1, Item2, Context: Pointer): Integer;
TListSortCustomItemExchanger_Context = procedure(Item1, Item2, Context: Pointer);
TPtrListSorter_Context = procedure(ItemPtrs: PPointer; ItemCount: SizeUInt; Comparer: TListSortComparer_Context; Context: Pointer);
TItemListSorter_Context = procedure(Items: Pointer; ItemCount, ItemSize: SizeUInt; Comparer: TListSortComparer_Context; Context: Pointer);
TItemListSorter_CustomItemExchanger_Context = procedure(Items: Pointer;
ItemCount, ItemSize: SizeUInt; Comparer: TListSortComparer_Context;
Exchanger: TListSortCustomItemExchanger_Context; Context: Pointer);
PSortingAlgorithm = ^TSortingAlgorithm;
TSortingAlgorithm = record
PtrListSorter_NoContextComparer: TPtrListSorter_NoContext;
PtrListSorter_ContextComparer: TPtrListSorter_Context;
ItemListSorter_ContextComparer: TItemListSorter_Context;
ItemListSorter_CustomItemExchanger_ContextComparer: TItemListSorter_CustomItemExchanger_Context;
end;
{
QuickSort
Average performance: O(n log n)
Worst performance: O(n*n)
Extra memory use: O(log n) on the stack
Stable: no
Additional notes: Uses the middle element as the pivot. This makes it work
well also on already sorted sequences, which can occur
often in practice. As expected from QuickSort, it works
best on random sequences and is usually the fastest
algorithm to sort them. It is, however, possible for a
malicious user to craft special sequences, which trigger
its worst O(n*n) case. They can also occur in practice,
although they are very unlikely. If this is not an
acceptable risk (e.g. for high risk applications,
security-conscious applications or applications with hard
real-time requirements), another sorting algorithm must
be used.
}
procedure QuickSort_PtrList_NoContext(
ItemPtrs: PPointer;
ItemCount: SizeUInt;
Comparer: TListSortComparer_NoContext);
procedure QuickSort_PtrList_Context(
ItemPtrs: PPointer;
ItemCount: SizeUInt;
Comparer: TListSortComparer_Context;
Context: Pointer);
procedure QuickSort_ItemList_Context(
Items: Pointer;
ItemCount, ItemSize: SizeUInt;
Comparer: TListSortComparer_Context;
Context: Pointer);
procedure QuickSort_ItemList_CustomItemExchanger_Context(
Items: Pointer;
ItemCount, ItemSize: SizeUInt;
Comparer: TListSortComparer_Context;
Exchanger: TListSortCustomItemExchanger_Context;
Context: Pointer);
const
QuickSort: TSortingAlgorithm = (
PtrListSorter_NoContextComparer: @QuickSort_PtrList_NoContext;
PtrListSorter_ContextComparer: @QuickSort_PtrList_Context;
ItemListSorter_ContextComparer: @QuickSort_ItemList_Context;
ItemListSorter_CustomItemExchanger_ContextComparer: @QuickSort_ItemList_CustomItemExchanger_Context;
);
var
DefaultSortingAlgorithm: PSortingAlgorithm = @QuickSort;
implementation
Procedure QuickSort_PtrList_NoContext(ItemPtrs: PPointer; L, R : SizeUInt;
Comparer: TListSortComparer_NoContext);
var
I, J, PivotIdx : SizeUInt;
P, Q : Pointer;
begin
repeat
I := L;
J := R;
PivotIdx := L + ((R - L) shr 1); { same as ((L + R) div 2), but without the possibility of overflow }
P := ItemPtrs[PivotIdx];
repeat
while (I < PivotIdx) and (Comparer(P, ItemPtrs[i]) > 0) do
Inc(I);
while (J > PivotIdx) and (Comparer(P, ItemPtrs[J]) < 0) do
Dec(J);
if I < J then
begin
Q := ItemPtrs[I];
ItemPtrs[I] := ItemPtrs[J];
ItemPtrs[J] := Q;
if PivotIdx = I then
begin
PivotIdx := J;
Inc(I);
end
else if PivotIdx = J then
begin
PivotIdx := I;
Dec(J);
end
else
begin
Inc(I);
Dec(J);
end;
end;
until I >= J;
// sort the smaller range recursively
// sort the bigger range via the loop
// Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
if (PivotIdx - L) < (R - PivotIdx) then
begin
if (L + 1) < PivotIdx then
QuickSort_PtrList_NoContext(ItemPtrs, L, PivotIdx - 1, Comparer);
L := PivotIdx + 1;
end
else
begin
if (PivotIdx + 1) < R then
QuickSort_PtrList_NoContext(ItemPtrs, PivotIdx + 1, R, Comparer);
if (L + 1) < PivotIdx then
R := PivotIdx - 1
else
exit;
end;
until L >= R;
end;
procedure QuickSort_PtrList_NoContext(ItemPtrs: PPointer; ItemCount: SizeUInt; Comparer: TListSortComparer_NoContext);
begin
if not Assigned(ItemPtrs) or (ItemCount < 2) then
exit;
QuickSort_PtrList_NoContext(ItemPtrs, 0, ItemCount - 1, Comparer);
end;
procedure QuickSort_PtrList_Context(ItemPtrs: PPointer; ItemCount: SizeUInt; Comparer: TListSortComparer_Context; Context: Pointer);
procedure QuickSort(L, R : SizeUInt);
var
I, J, PivotIdx : SizeUInt;
P, Q : Pointer;
begin
repeat
I := L;
J := R;
PivotIdx := L + ((R - L) shr 1); { same as ((L + R) div 2), but without the possibility of overflow }
P := ItemPtrs[PivotIdx];
repeat
while (I < PivotIdx) and (Comparer(P, ItemPtrs[I], Context) > 0) do
Inc(I);
while (J > PivotIdx) and (Comparer(P, ItemPtrs[J], Context) < 0) do
Dec(J);
if I < J then
begin
Q := ItemPtrs[I];
ItemPtrs[I] := ItemPtrs[J];
ItemPtrs[J] := Q;
if PivotIdx = I then
begin
PivotIdx := J;
Inc(I);
end
else if PivotIdx = J then
begin
PivotIdx := I;
Dec(J);
end
else
begin
Inc(I);
Dec(J);
end;
end;
until I >= J;
// sort the smaller range recursively
// sort the bigger range via the loop
// Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
if (PivotIdx - L) < (R - PivotIdx) then
begin
if (L + 1) < PivotIdx then
QuickSort(L, PivotIdx - 1);
L := PivotIdx + 1;
end
else
begin
if (PivotIdx + 1) < R then
QuickSort(PivotIdx + 1, R);
if (L + 1) < PivotIdx then
R := PivotIdx - 1
else
exit;
end;
until L >= R;
end;
begin
if not Assigned(ItemPtrs) or (ItemCount < 2) then
exit;
QuickSort(0, ItemCount - 1);
end;
procedure QuickSort_ItemList_Context(Items: Pointer; ItemCount, ItemSize: SizeUInt; Comparer: TListSortComparer_Context; Context: Pointer);
var
TempBuf: Pointer;
procedure QuickSort(L, R : SizeUInt);
var
I, J, PivotIdx : SizeUInt;
P : Pointer;
begin
repeat
I := L;
J := R;
PivotIdx := L + ((R - L) shr 1); { same as ((L + R) div 2), but without the possibility of overflow }
P := Items + ItemSize*PivotIdx;
repeat
while (I < PivotIdx) and (Comparer(P, Items + ItemSize*I, Context) > 0) do
Inc(I);
while (J > PivotIdx) and (Comparer(P, Items + ItemSize*J, Context) < 0) do
Dec(J);
if I < J then
begin
Move((Items + ItemSize*I)^, TempBuf^, ItemSize);
Move((Items + ItemSize*J)^, (Items + ItemSize*I)^, ItemSize);
Move(TempBuf^, (Items + ItemSize*J)^, ItemSize);
if PivotIdx = I then
begin
PivotIdx := J;
P := Items + ItemSize*PivotIdx;
Inc(I);
end
else if PivotIdx = J then
begin
PivotIdx := I;
P := Items + ItemSize*PivotIdx;
Dec(J);
end
else
begin
Inc(I);
Dec(J);
end;
end;
until I >= J;
// sort the smaller range recursively
// sort the bigger range via the loop
// Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
if (PivotIdx - L) < (R - PivotIdx) then
begin
if (L + 1) < PivotIdx then
QuickSort(L, PivotIdx - 1);
L := PivotIdx + 1;
end
else
begin
if (PivotIdx + 1) < R then
QuickSort(PivotIdx + 1, R);
if (L + 1) < PivotIdx then
R := PivotIdx - 1
else
exit;
end;
until L >= R;
end;
begin
if not Assigned(Items) or (ItemCount < 2) or (ItemSize < 1) then
exit;
GetMem(TempBuf, ItemSize);
{$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
try
QuickSort(0, ItemCount - 1);
finally
FreeMem(TempBuf, ItemSize);
end;
{$else FPC_HAS_FEATURE_EXCEPTIONS}
QuickSort(0, ItemCount - 1);
FreeMem(TempBuf, ItemSize);
{$endif FPC_HAS_FEATURE_EXCEPTIONS}
end;
procedure QuickSort_ItemList_CustomItemExchanger_Context(
Items: Pointer;
ItemCount, ItemSize: SizeUInt;
Comparer: TListSortComparer_Context;
Exchanger: TListSortCustomItemExchanger_Context;
Context: Pointer);
procedure QuickSort(L, R : SizeUInt);
var
I, J, PivotIdx : SizeUInt;
P : Pointer;
begin
repeat
I := L;
J := R;
PivotIdx := L + ((R - L) shr 1); { same as ((L + R) div 2), but without the possibility of overflow }
P := Items + ItemSize*PivotIdx;
repeat
while (I < PivotIdx) and (Comparer(P, Items + ItemSize*I, Context) > 0) do
Inc(I);
while (J > PivotIdx) and (Comparer(P, Items + ItemSize*J, Context) < 0) do
Dec(J);
if I < J then
begin
Exchanger(Items + ItemSize*I, Items + ItemSize*J, Context);
if PivotIdx = I then
begin
PivotIdx := J;
P := Items + ItemSize*PivotIdx;
Inc(I);
end
else if PivotIdx = J then
begin
PivotIdx := I;
P := Items + ItemSize*PivotIdx;
Dec(J);
end
else
begin
Inc(I);
Dec(J);
end;
end;
until I >= J;
// sort the smaller range recursively
// sort the bigger range via the loop
// Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
if (PivotIdx - L) < (R - PivotIdx) then
begin
if (L + 1) < PivotIdx then
QuickSort(L, PivotIdx - 1);
L := PivotIdx + 1;
end
else
begin
if (PivotIdx + 1) < R then
QuickSort(PivotIdx + 1, R);
if (L + 1) < PivotIdx then
R := PivotIdx - 1
else
exit;
end;
until L >= R;
end;
begin
if not Assigned(Items) or (ItemCount < 2) or (ItemSize < 1) then
exit;
QuickSort(0, ItemCount - 1);
end;
end.
|