Skip to content

Commit aa3a411

Browse files
committed
quick sort implementation
1 parent 0b10c8a commit aa3a411

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,25 @@ for i=0,(a:size()-1) do
279279
end
280280
```
281281

282+
### Sorting (Quick Sort)
283+
284+
```lua
285+
local list = require("lualgorithms.data.list")
286+
local a = list.create()
287+
a:add(100)
288+
a:add(200)
289+
a:add(300)
290+
a:add(600)
291+
a:add(200)
292+
a:add(400)
293+
a:add(340)
294+
a:add(120)
295+
a:add(10)
296+
297+
local quicksort = require("lualgorithms.sorting.quicksort")
298+
quicksort.sort(a, function(a1, a2) return a1 - a2 end)
299+
300+
for i=0,(a:size()-1) do
301+
print(a:get(i))
302+
end
303+
```

rockspecs/lualgorithms-1.0-5.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ build = {
3232
["lualgorithms.sorting.insertion"] = "src/sorting/insertion.lua",
3333
["lualgorithms.sorting.shellsort"] = "src/sorting/shellsort.lua",
3434
["lualgorithms.sorting.mergesort"] = "src/sorting/mergesort.lua",
35+
["lualgorithms.sorting.quicksort"] = "src/sorting/quicksort.lua",
3536
}
3637
}

spec/quick_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("Merge 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 quicksort = require("sorting.quicksort")
24+
quicksort.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/quicksort.lua

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: chen0
4+
-- Date: 29/6/2017
5+
-- Time: 10:04 AM
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
9+
local quicksort = {}
10+
quicksort.__index = quicksort
11+
12+
function quicksort.sort(a, comparator)
13+
quicksort._sort(a, 0, a:size()-1, comparator)
14+
end
15+
16+
function quicksort._sort(a, lo, hi, comparator)
17+
if lo >= hi then
18+
return
19+
end
20+
21+
local j = quicksort._partition(a, lo, hi, comparator)
22+
quicksort._sort(a, lo, j-1, comparator)
23+
quicksort._sort(a, j+1, hi, comparator)
24+
end
25+
26+
function quicksort._partition(a, lo, hi, comparator)
27+
local i = lo
28+
local j = hi + 1
29+
local v = a:get(lo)
30+
while true do
31+
i = i + 1
32+
while comparator(a:get(i), v) < 0 do
33+
i = i+1
34+
if i >= hi then
35+
break
36+
end
37+
end
38+
j = j - 1
39+
while comparator(v, a:get(j)) < 0 do
40+
j = j - 1
41+
if j <= lo then
42+
break
43+
end
44+
end
45+
46+
if i >= j then
47+
break
48+
end
49+
50+
quicksort.exchange(a, i, j)
51+
end
52+
quicksort.exchange(a, lo, j)
53+
return j
54+
end
55+
56+
function quicksort.exchange(a, i, j)
57+
local temp = a:get(i)
58+
a:set(i, a:get(j))
59+
a:set(j, temp)
60+
end
61+
62+
return quicksort

0 commit comments

Comments
 (0)