-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathstabilize.lua
34 lines (34 loc) · 964 Bytes
/
stabilize.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
-- Stabilizes any sorting algorithm, using linear auxiliary space (for the indices) per sorting
return function(
sort -- function(list, less_than)
)
-- stabilized sorting function
return function(list, less_than)
less_than = less_than or function(a, b)
return a < b
end
-- Build a list of indices
local indices = {}
for index = 1, #list do
indices[index] = index
end
-- Sort the list of indices according to values; compare indices only if they have the same value
sort(indices, function(index_a, index_b)
if less_than(list[index_a], list[index_b]) then
return true
end
if less_than(list[index_b], list[index_a]) then
return false
end
return index_a < index_b
end)
-- Map indices to values
for index = 1, #list do
indices[index] = list[indices[index]]
end
-- Replace elements in original list (sorting is supposed to be in-place)
for index = 1, #list do
list[index] = indices[index]
end
end
end