Skip to content

Commit 976c13a

Browse files
committed
Merge pull request kennyledet#98 from Yonaba/master
Moved .lua files to subfolder
2 parents 049a887 + eabe8cf commit 976c13a

4 files changed

Lines changed: 83 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- Insertion sort algorithm
2+
-- See: http://en.wikipedia.org/wiki/Insertion_sort#Algorithm
3+
4+
-- list: a list to be sorted, in-place
5+
-- comp: (optional) a comparison function
6+
-- defaults to function(a, b) return a < b end
7+
-- returns: the passed-in list, sorted
8+
return function (list, comp)
9+
comp = comp or function(a, b) return a < b end
10+
local n = #list
11+
for i = 2, n do
12+
local x = list[i]
13+
local j = i
14+
while j>1 and not comp(list[j-1],x) do
15+
list[j] = list[j-1]
16+
j = j - 1
17+
end
18+
list[j] = x
19+
end
20+
return list
21+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- Tests for insertion_sort.lua
2+
local insertion_sort = require 'insertion_sort'
3+
4+
local total, pass = 0, 0
5+
6+
local function dec(str, len)
7+
return #str < len
8+
and str .. (('.'):rep(len-#str))
9+
or str:sub(1,len)
10+
end
11+
12+
local function run(message, f)
13+
total = total + 1
14+
local ok, err = pcall(f)
15+
if ok then pass = pass + 1 end
16+
local status = ok and 'PASSED' or 'FAILED'
17+
print(('%02d. %68s: %s'):format(total, dec(message,68), status))
18+
end
19+
20+
-- Comparison functions
21+
local function le(a,b) return a <= b end
22+
local function ge(a,b) return a >= b end
23+
24+
-- Checks if list is sorted
25+
function is_sorted(list, comp)
26+
comp = comp or le
27+
for i = 2, #list do
28+
if not comp(list[i-1],list[i]) then return false end
29+
end
30+
return true
31+
end
32+
33+
-- Generates a table of n random values
34+
local function gen(n)
35+
local t = {}
36+
for i = 1, n do t[i] = math.random(n) end
37+
return t
38+
end
39+
40+
math.randomseed(os.time())
41+
42+
run('Empty arrays', function()
43+
local t = {}
44+
assert(is_sorted(insertion_sort({})))
45+
end)
46+
47+
run('Already sorted array', function()
48+
local t = {1, 2, 3, 4, 5}
49+
assert(is_sorted(insertion_sort(t)))
50+
end)
51+
52+
run('Sorting a large array (1e3 values)', function()
53+
local t = gen(1e3)
54+
assert(is_sorted(insertion_sort(t)))
55+
assert(is_sorted(insertion_sort(t, ge), ge))
56+
end)
57+
58+
print(('-'):rep(80))
59+
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
60+
:format(total, pass, total-pass, (pass*100/total)))
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ end
3030

3131
-- Quicksort function wrapper, to shadow access
3232
-- to left and right bounds
33-
-- list: a list to be ordered
33+
-- list: a list to be ordered, in-place
3434
-- comp: (optional) a comparison function
3535
-- defaults to function(a, b) return a < b end
36+
-- returns: the passed-in list, sorted
3637
return function (list, comp)
3738
comp = comp or function(a, b) return a < b end
3839
return quicksort(list, comp)

0 commit comments

Comments
 (0)