Skip to content

Commit d417988

Browse files
committed
insertion sort
1 parent 7cbee2d commit d417988

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,26 @@ for i=0,(a:size()-1) do
191191
print(a:get(i))
192192
end
193193
```
194+
195+
### Sorting (Insertion Sort)
196+
197+
```lua
198+
local list = require("lualgorithms.data.list")
199+
local a = list.create()
200+
a:add(100)
201+
a:add(200)
202+
a:add(300)
203+
a:add(600)
204+
a:add(200)
205+
a:add(400)
206+
a:add(340)
207+
a:add(120)
208+
a:add(10)
209+
210+
local insertion = require("lualgorithms.sorting.insertion")
211+
insertion.sort(a, function(a1, a2) return a1 - a2 end)
212+
213+
for i=0,(a:size()-1) do
214+
print(a:get(i))
215+
end
216+
```

note/publish.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# follow this link: https://github.com/luarocks/luarocks/wiki/creating-a-rock
22

3-
git tag v1.0.4
3+
git tag v1.0.5
44
git push --tags
55

6-
luarocks publish rockspecs/lualgorithms-1.0-4.rockspec --api-key=<api-key>
6+
luarocks publish rockspecs/lualgorithms-1.0-5.rockspec --api-key=<api-key>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package = "lualgorithms"
2+
version = "1.0-5"
3+
source = {
4+
url = "git://github.com/chen0040/lua-algorithms.git",
5+
tag = "v1.0.5",
6+
}
7+
description = {
8+
summary = "Lua Algorithms Library",
9+
detailed = [[
10+
Lua implementation for algorithms and data structures
11+
found in Java
12+
]],
13+
homepage = "https://github.com/chen0040/lua-algorithms",
14+
license = "MIT/X11"
15+
}
16+
dependencies = {
17+
"lua >= 5.1, < 5.4"
18+
}
19+
build = {
20+
type = "builtin",
21+
modules = {
22+
-- Note the required Lua syntax when listing submodules as keys
23+
["lualgorithms.data.stack"] = "src/data/stack.lua",
24+
["lualgorithms.data.list"] = "src/data/list.lua",
25+
["lualgorithms.data.queue"] = "src/data/queue.lua",
26+
["lualgorithms.data.minpq"] = "src/data/minpq.lua",
27+
["lualgorithms.data.maxpq"] = "src/data/maxpq.lua",
28+
["lualgorithms.data.hashmap"] = "src/data/hashmap.lua",
29+
["lualgorithms.data.redblacktree"] = "src/data/redblacktree.lua",
30+
["lualgorithms.sorting.selection"] = "src/sorting/selection.lua",
31+
["lualgorithms.sorting.insertion"] = "src/sorting/insertion.lua",
32+
}
33+
}

spec/insertion_sort_spec.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: chen0
4+
-- Date: 28/6/2017
5+
-- Time: 9:16 AM
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
9+
describe("Insertion Sort", function()
10+
it("should sort ascendingly", function()
11+
local list = require("data.list")
12+
local a = list.create()
13+
a:add(100)
14+
a:add(200)
15+
a:add(300)
16+
a:add(600)
17+
a:add(200)
18+
a:add(400)
19+
a:add(340)
20+
a:add(120)
21+
a:add(10)
22+
23+
local insertion = require("sorting.insertion")
24+
insertion.sort(a, function(a1, a2) return a1 - a2 end)
25+
26+
for i=0,(a:size()-2) do
27+
assert.is_true(a:get(i) <= a:get(i+1))
28+
end
29+
30+
end)
31+
end)
32+

src/sorting/insertion.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: chen0
4+
-- Date: 28/6/2017
5+
-- Time: 10:44 AM
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
9+
insertion = {}
10+
insertion.__index = insertion
11+
12+
function insertion.sort(a, comparator)
13+
local N = a:size()
14+
for i=1,(N-1) do
15+
for j=(i-1),0,-1 do
16+
if comparator(a:get(j+1), a:get(j)) < 0 then
17+
insertion.exchange(a, j, j+1)
18+
else
19+
break
20+
end
21+
end
22+
end
23+
end
24+
25+
function insertion.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 insertion

0 commit comments

Comments
 (0)