We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e204c16 commit 1c8a514Copy full SHA for 1c8a514
sorting/shellsort.lua
@@ -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
19
20
21
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
30
31
+return shellsort
0 commit comments