Skip to content

Commit b2e88ad

Browse files
committed
Merge pull request kennyledet#104 from Yonaba/master
Bit of cleanup and Shellsort
2 parents c0cffc6 + cd4ba22 commit b2e88ad

3 files changed

Lines changed: 83 additions & 1 deletion

File tree

Selection_Sort/Lua/Yonaba/selection_sort_test.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ math.randomseed(os.time())
4141

4242
run('Empty arrays', function()
4343
local t = {}
44-
print(is_sorted(selection_sort({})))
4544
assert(is_sorted(selection_sort({})))
4645
end)
4746

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- Shell sort algorithm
2+
-- See: http://en.wikipedia.org/wiki/Shellsort
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 gap = 1, n do
12+
for i = gap, n do
13+
local tmp = list[i]
14+
local j = i
15+
while j > gap and comp(tmp, list[j-gap]) do
16+
list[j] = list[j-gap]
17+
j = j - gap
18+
end
19+
list[j] = tmp
20+
end
21+
end
22+
return list
23+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- Tests for shell_sort.lua
2+
local shell_sort = require 'shell_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(shell_sort({})))
45+
end)
46+
47+
run('Already sorted array', function()
48+
local t = {1, 2, 3, 4, 5}
49+
assert(is_sorted(shell_sort(t)))
50+
end)
51+
52+
run('Sorting a large array (1e3 values)', function()
53+
local t = gen(1e3)
54+
assert(is_sorted(shell_sort(t)))
55+
assert(is_sorted(shell_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)))

0 commit comments

Comments
 (0)