Skip to content

Commit 1c8a514

Browse files
authored
Create shellsort.lua
1 parent e204c16 commit 1c8a514

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

sorting/shellsort.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local shellsort = {}
2+
shellsort.__index = shellsort
3+
4+
function shellsort.sort(a, comparator)
5+
local N = a:size()
6+
local h = 0
7+
while h < math.floor(N / 3) do
8+
h = h * 3 + 1
9+
end
10+
11+
for step=h,1,-1 do
12+
for i=step,(N-1) do
13+
for j=i,step,-step do
14+
if comparator(a:get(j), a:get(j-step)) < 0 then
15+
shellsort.exchange(a, j, j-step)
16+
else
17+
break
18+
end
19+
end
20+
end
21+
end
22+
23+
end
24+
25+
function shellsort.exchange(a, i, j)
26+
local temp = a:get(i)
27+
a:set(i, a:get(j))
28+
a:set(j, temp)
29+
end
30+
31+
return shellsort

0 commit comments

Comments
 (0)