-
Notifications
You must be signed in to change notification settings - Fork 3
/
Sorting.pas
448 lines (381 loc) · 13.2 KB
/
Sorting.pas
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
{
Copyright (c) Peter Karpov 2010 - 2017.
Usage of the works is permitted provided that this instrument is retained with
the works, so that any entity that uses the works is notified of this instrument.
DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
}
{$IFDEF FPC} {$MODE DELPHI} {$ENDIF}
unit Sorting; ///////////////////////////////////////////////////////////////////////
{
>> Version: 2.0
>> Description
Universal implementations of insertion sort and Shellsort plus predefined routines
for integer and real arrays. Shellsort gap sequence taken from RosettaCode.com.
Part of InvLibs unit collection.
>> Usage
Call a sorting procedure with a pointer to your data, pointers to comparison and
swap procedures (see definitions), number of records to sort and the desired
order.
>> Author
Peter Karpov
Email : [email protected]
Homepage : inversed.ru
GitHub : inversed-ru
Twitter : @inversed_ru
>> ToDo
? Quicksort
? Versions that accept a scalar value function instead of a comparison function
>> Changelog
2.0 : 2017.11.27 ~ Renamed TSortOrder values
- RealArrayCompare function removed from the interface
- WrongOrder function replaced with an array
~ Rewrote sorting using Shellsort single pass functions
+ DeleteDuplicates procedures
~ Comments cleanup
~ FreePascal compatibility
1.7 : 2015.06.26 + OrderRealArray procedure
1.6 : 2014.10.11 + OrderIntArray procedure
1.5.3 : 2011.10.14 ~ Moved to generic types
* SortInt fixed
1.5.1 : 2011.07.27 * Sort procedure fixed
1.5 : 2011.06.14 ~ 'Array sorting' section rewrite
1.4 : 2011.03.27 + Added insertion sort
* Fixed SortOrder
1.3 : 2011.03.18 ~ SimpleStruct 1.4 compatibility
* Fixed SortOrder
1.0 : 2010.11.14 ~ Changed combsort to Shellsort with a good gap sequence
+ Added procedure that returns element ordering, but doesn't
actually sort the data
0.0 : 2010.05.05 + Initial version, basic combsort
Notation: + added, - removed, * fixed, ~ changed
}
interface ///////////////////////////////////////////////////////////////////////////
uses
Arrays;
type
TSortOrder = (soAscending, soDescending);
TComparison = (cmpLess, cmpGreater, cmpEqual);
ProcCompare =
function(
ptrData : Pointer;
i, j : Integer
) : TComparison;
ProcSwap =
procedure(
ptrData : Pointer;
i, j : Integer);
{-----------------------<< Shell sort >>--------------------------------------------}
// Sort the data of length N located at ptrData using Compare and Swap routines via
// Shellsort in the specified Order
procedure Sort(
ptrData : Pointer;
Compare : ProcCompare;
Swap : ProcSwap;
N : Integer;
Order : TSortOrder);
// Return the Ordering of the elements in the array of length N located at ptrData
// sorted via Shellsort in the specified Order
procedure SortOrder(
var Ordering : TIntArray;
ptrData : Pointer;
Compare : ProcCompare;
N : Integer;
Order : TSortOrder);
{-----------------------<< Insertion sort >>----------------------------------------}
// Sort the data of length N located at ptrData using Compare and Swap routines via
// isertion sort in the specified Order
procedure InsertionSort(
ptrData : Pointer;
Compare : ProcCompare;
Swap : ProcSwap;
N : Integer;
Order : TSortOrder);
// Return the Ordering of the elements in the array of length N located at ptrData
// sorted via isertion sort in the specified Order
procedure InsertionSortOrder(
var Ordering : TIntArray;
ptrData : Pointer;
Compare : ProcCompare;
N : Integer;
Order : TSortOrder);
{-----------------------<< Predefined Sorting Procedures >>-------------------------}
// Sort A according to Order
procedure SortRealArray(
var A : TRealArray;
Order : TSortOrder);
procedure SortRealArrayN(
var A : TRealArrayN;
Order : TSortOrder);
procedure SortIntArray(
var A : TIntArray;
Order : TSortOrder);
procedure SortIntArrayN(
var A : TIntArrayN;
Order : TSortOrder);
// Return the Ordering of elements of A sorted according to Order
procedure OrderIntArray(
var Ordering : TIntArray;
const A : TIntArray;
Order : TSortOrder);
procedure OrderRealArray(
var Ordering : TIntArray;
const A : TRealArray;
Order : TSortOrder);
{-----------------------<< Duplicate removal >>-------------------------------------}
// Return all distinct elements of A sorted in ascending order
procedure DeleteDuplicates(
var A : TIntArray);
overload;
procedure DeleteDuplicates(
var A : TIntArrayN);
overload;
implementation //////////////////////////////////////////////////////////////////////
const
NGaps = 24;
Gaps : array [0 .. NGaps - 1] of Integer = (
1, 3, 7, 17, 43, 107, 269, 673, 1693, 4231, 10567, 26417, 66041, 165103,
412771, 1031923, 2579807, 6449537, 16123841, 40309589, 100773961, 251934929,
629837311, 1574593277);
WrongCmp : array [TSortOrder] of TComparison = (cmpGreater, cmpLess);
{-----------------------<< Shell sort >>--------------------------------------------}
// Perform a single Shellsort pass with given Gap and Order over the data of length N
// located at ptrData using Compare and Swap routines
procedure ShellsortPass(
ptrData : Pointer;
Compare : ProcCompare;
Swap : ProcSwap;
N, Gap : Integer;
Order : TSortOrder);
var
i, j : Integer;
begin
for i := Gap to N - 1 do
begin
j := i;
while (j >= Gap) and
(Compare(ptrData, j - Gap, j) = WrongCmp[Order]) do
begin
Swap(ptrData, j, j - Gap);
j := j - Gap;
end;
end;
end;
// Perform a single Shellsort pass with given Gap and Order over the Ordering of data
// of length N located at ptrData using Compare function
procedure ShellsortPassOrder(
var Ordering : TIntArray;
ptrData : Pointer;
Compare : ProcCompare;
N, Gap : Integer;
Order : TSortOrder);
var
i, j : Integer;
Temp : Integer;
begin
for i := Gap to N - 1 do
begin
j := i;
Temp := Ordering[i];
while (j >= Gap) and
(Compare(ptrData, Ordering[j - Gap], Temp) = WrongCmp[Order]) do
begin
Ordering[j] := Ordering[j - Gap];
j := j - Gap;
end;
Ordering[j] := Temp;
end;
end;
// Sort the data of length N located at ptrData using Compare and Swap routines via
// Shellsort in the specified Order
procedure Sort(
ptrData : Pointer;
Compare : ProcCompare;
Swap : ProcSwap;
N : Integer;
Order : TSortOrder);
var
k, Gap : Integer;
begin
k := NGaps;
repeat
Dec(k);
Gap := Gaps[k];
ShellsortPass(ptrData, Compare, Swap, N, Gap, Order);
until Gap = 1;
end;
// Return the Ordering of the elements in the array of length N located at ptrData
// sorted via Shellsort in the specified Order
procedure SortOrder(
var Ordering : TIntArray;
ptrData : Pointer;
Compare : ProcCompare;
N : Integer;
Order : TSortOrder);
var
k, Gap : Integer;
begin
SequentialFill(Ordering, N, {Base:} 0);
k := NGaps;
repeat
Dec(k);
Gap := Gaps[k];
ShellsortPassOrder(Ordering, ptrData, Compare, N, Gap, Order);
until Gap = 1;
end;
{-----------------------<< Insertion sort >>----------------------------------------}
// Sort the data of length N located at ptrData using Compare and Swap routines via
// isertion sort in the specified Order
procedure InsertionSort(
ptrData : Pointer;
Compare : ProcCompare;
Swap : ProcSwap;
N : Integer;
Order : TSortOrder);
begin
ShellsortPass(ptrData, Compare, Swap, N, {Gap:} 1, Order);
end;
// Return the Ordering of the elements in the array of length N located at ptrData
// sorted via isertion sort in the specified Order
procedure InsertionSortOrder(
var Ordering : TIntArray;
ptrData : Pointer;
Compare : ProcCompare;
N : Integer;
Order : TSortOrder);
begin
ShellsortPassOrder(Ordering, ptrData, Compare, N, {Gap:} 1, Order);
end;
{-----------------------<< Predefined Compare and Swap Routines>>-------------------}
// #COPYPASTA
// Compare i-th and j-th elements of a real array located at ptrData
function RealArrayCompare(
ptrData : Pointer;
i, j : Integer
) : TComparison;
var
ptrArray : PRealArray;
x1, x2 : Real;
begin
ptrArray := PRealArray(ptrData);
x1 := ptrArray^[i];
x2 := ptrArray^[j];
if x1 > x2 then
Result := cmpGreater
else if x1 < x2 then
Result := cmpLess
else
Result := cmpEqual;
end;
// Swap i-th and j-th elements of a real array located at ptrData
procedure RealArraySwap(
ptrData : Pointer;
i, j : Integer);
var
ptrArray : PRealArray;
Temp : Real;
begin
ptrArray := PRealArray(ptrData);
Temp := ptrArray^[i];
ptrArray^[i] := ptrArray^[j];
ptrArray^[j] := Temp;
end;
// Compare i-th and j-th elements of an integer array located at ptrData
function IntArrayCompare(
ptrData : Pointer;
i, j : Integer
) : TComparison;
var
ptrArray : PIntArray;
x1, x2 : Integer;
begin
ptrArray := PIntArray(ptrData);
x1 := ptrArray^[i];
x2 := ptrArray^[j];
if x1 > x2 then
Result := cmpGreater
else if x1 < x2 then
Result := cmpLess
else
Result := cmpEqual;
end;
// Swap i-th and j-th elements of an integer array located at ptrData
procedure IntArraySwap(
ptrData : Pointer;
i, j : Integer);
var
ptrArray : PIntArray;
Temp : Integer;
begin
ptrArray := PIntArray(ptrData);
Temp := ptrArray^[i];
ptrArray^[i] := ptrArray^[j];
ptrArray^[j] := Temp;
end;
{-----------------------<< Predefined Sorting Procedures >>-------------------------}
// Sort A according to Order
procedure SortRealArray(
var A : TRealArray;
Order : TSortOrder);
begin
Sort(@A, RealArrayCompare, RealArraySwap, Length(A), Order);
end;
procedure SortRealArrayN(
var A : TRealArrayN;
Order : TSortOrder);
begin
Sort(@A._, RealArrayCompare, RealArraySwap, A.N, Order);
end;
procedure SortIntArray(
var A : TIntArray;
Order : TSortOrder);
begin
Sort(@A, IntArrayCompare, IntArraySwap, Length(A), Order);
end;
procedure SortIntArrayN(
var A : TIntArrayN;
Order : TSortOrder);
begin
Sort(@A._, IntArrayCompare, IntArraySwap, A.N, Order);
end;
// Return the Ordering of elements of A sorted according to Order
procedure OrderIntArray(
var Ordering : TIntArray;
const A : TIntArray;
Order : TSortOrder);
begin
SortOrder(Ordering, @A, IntArrayCompare, Length(A), Order);
end;
procedure OrderRealArray(
var Ordering : TIntArray;
const A : TRealArray;
Order : TSortOrder);
begin
SortOrder(Ordering, @A, RealArrayCompare, Length(A), Order);
end;
{-----------------------<< Duplicate removal >>-------------------------------------}
// Return all distinct elements of A sorted in ascending order
procedure DeleteDuplicates(
var A : TIntArray);
overload;
var
N, i : Integer;
begin
SortIntArray(A, soAscending);
N := 1;
for i := 1 to Length(A) - 1 do
if A[i] <> A[i - 1] then
begin
A[N] := A[i];
Inc(N);
end;
SetLength(A, N);
end;
procedure DeleteDuplicates(
var A : TIntArrayN);
overload;
begin
SetTrueLength(A);
DeleteDuplicates(A._);
A.N := Length(A._);
end;
end.