-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.lua
448 lines (244 loc) · 6.8 KB
/
sort.lua
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
local function medianOf3(t, low, high, comp)
local middle = math.floor( ( low + high ) / 2 )
if comp(t[middle], t[low]) then
t[middle], t[low] = t[low], t[middle]
end
if comp(t[high], t[low]) then
t[high], t[low] = t[low], t[high]
end
if comp(t[high], t[middle]) then
t[high], t[middle] = t[middle], t[high]
end
t[middle], t[high - 1] = t[high - 1], t[middle]
return t[high - 1]
end
local function partition(t, low, high, pivot, comp)
local left = low - 1
local right = high + 1
repeat
repeat
left = left + 1
until (not comp(t[left], pivot)) or left >= high
repeat
right = right - 1
until (not comp(pivot, t[right])) or right <= low
if left >= right then
break
else
t[left], t[right] = t[right], t[left]
end
until false
return left
end
local function insertionSort(t, left, right, comp)
local left = left or 1
local right = right or #t
for i = left + 1, right do
local aux = t[i]
local j = i
while j > left and not comp(t[j - 1], aux) do
t[j] = t[j - 1]
j = j - 1
end
t[j] = aux
end
end
local function quickSort(t, left, right, comp)
local size = right - left + 1
if size < 10 then
insertionSort(t, left, right, comp)
else
local pivot = medianOf3(t, left, right, comp)
local par = partition(t, left, right, pivot, comp)
quickSort(t, left, par - 1, comp)
quickSort(t, par, right, comp)
end
end
local function selectionSort(t, left, right, comp)
for i = left, right do
local aux = i
for j = i, right do
if comp(t[j], t[aux]) then
aux = j
end
end
t[i], t[aux] = t[aux], t[i]
end
end
local function compare(a, b)
return a < b
end
function table.insertionsort(t, comp)
insertionSort(t, 1, #t, comp or compare)
end
function table.quicksort(t, comp)
quickSort(t, 1, #t, comp or compare)
end
function table.selectionsort(t, comp)
selectionSort(t, 1, #t, comp or compare)
end
local hashFunctions = {}
function hashFunctions.number(Number)
return Number
end
function hashFunctions.string(String)
local Hash = 0
for i = 1, #String do
Hash = Hash * 31 + String:byte(i)
end
return Hash
end
function table.countingsort(Table, hashFunction)
if not hashFunction then
local Index, Value = next(Table)
if not Value then
return nil
end
hashFunction = hashFunctions[type(Value)]
end
local Max
local Min
local Hash = {}
local Copy = {}
for i = 1, #Table do
local Value = Table[i]
if not Hash[Value] then
Hash[Value] = hashFunction(Value)
end
Max = math.max(Hash[Value], Max or Hash[Value])
Min = math.min(Hash[Value], Min or Hash[Value])
Copy[i] = Value
end
local CountingArray = {}
for i = 1, #Table do
local Index = Hash[Table[i]] - Min + 1
local Count = CountingArray[Index]
if not Count then
Count = 0
end
CountingArray[Index] = Count + 1
end
local Total = 1
for i = 1, Max - Min + 1 do
local OldCount = CountingArray[i]
if not OldCount then
OldCount = 0
end
CountingArray[i] = Total
Total = Total + OldCount
end
for i = 1, #Copy do
local Index = Hash[Copy[i]] - Min + 1
local TableIndex = CountingArray[Index]
Table[TableIndex] = Copy[i]
CountingArray[Index] = TableIndex + 1
end
end
function table.countingsort2(Table, hashFunction)
if not hashFunction then
local Index, Value = next(Table)
if not Value then
return nil
end
hashFunction = hashFunctions[type(Value)]
end
local Max
local Min
local Hash = {}
local Copy = {}
for i = 1, #Table do
local Value = Table[i]
if not Hash[Value] then
Hash[Value] = hashFunction(Value)
end
Max = math.max(Hash[Value], Max or Hash[Value])
Min = math.min(Hash[Value], Min or Hash[Value])
Copy[i] = Value
end
local CountingArray = {}
for i = 1, #Table do
local Index = Hash[Table[i]] - Min + 1
local Count = CountingArray[Index]
if not Count then
Count = 0
end
CountingArray[Index] = Count + 1
end
local Total = 1
for i = 1, Max - Min + 1 do
local OldCount = CountingArray[i]
if not OldCount then
OldCount = 0
end
CountingArray[i] = Total
Total = Total + OldCount
end
for i = 1, #Copy do
local Index = Hash[Copy[i]] - Min + 1
local TableIndex = CountingArray[Index]
Table[TableIndex] = Copy[i]
CountingArray[Index] = TableIndex + 1
end
end
function table.distributionSort(Table, Start, End, hashFunction, comp)
if not hashFunction then
local Index, Value = next(Table)
if not Value then
return nil
end
hashFunction = hashFunctions[type(Value)]
end
local Start = Start or 1
local End = End or #Table
local Max
local Min
local Hash = {}
for i = Start, End do
local Value = Table[i]
if not Hash[Value] then
Hash[Value] = hashFunction(Value)
end
Max = math.max(Hash[Value], Max or Hash[Value])
Min = math.min(Hash[Value], Min or Hash[Value])
end
local Distribution = {}
local DistributionPos = {}
local DistributionCount = {}
local DistributionLength = math.ceil( math.sqrt( End - Start + 1 ) )
local Factor = DistributionLength / ( Max - Min + 1 )
for i = Start, End do
local HashIndex = Hash[Table[i]] - Min
local Index = math.floor( HashIndex * Factor ) + 1
Distribution[ Index ] = ( Distribution[ Index ] or 0 ) + 1
end
local Accum = Start
for i = 1, DistributionLength + 1 do
DistributionCount[i] = Distribution[i] or 0
DistributionPos[i] = Accum
Distribution[i] = Accum
Accum = Accum + DistributionCount[i]
end
for DistributionIndex = 1, DistributionLength do
for i = Distribution[DistributionIndex], Distribution[DistributionIndex + 1] - 1 do
local HashIndex = Hash[Table[i]] - Min
local Index = math.floor( HashIndex * Factor ) + 1
while Index ~= DistributionIndex do
local AuxIndex = DistributionPos[Index]
local Aux = Table[AuxIndex]
DistributionPos[Index] = DistributionPos[Index] + 1
Table[AuxIndex] = Table[i]
Table[i] = Aux
HashIndex = Hash[Table[i]] - Min
Index = math.floor( HashIndex * Factor ) + 1
end
end
if DistributionCount[DistributionIndex] > 1 then
if DistributionCount[DistributionIndex] < 10 then
insertionSort(Table, Distribution[DistributionIndex], Distribution[DistributionIndex] + DistributionCount[DistributionIndex] - 1, comp or compare)
else
table.distributionSort(Table, Distribution[DistributionIndex], Distribution[DistributionIndex] + DistributionCount[DistributionIndex] - 1, hashFunction, comp or compare)
end
end
end
return nil
end