-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsort.lua
105 lines (86 loc) · 2.13 KB
/
sort.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
MACHINE=os.getenv('MACHINE')
LANGUAGE="lua"
json = require('json')
local f = assert(io.open("generated_data.json", "rb"))
local content = f:read("*all")
f:close()
local data = json.decode(content)
function array_copy(a)
local b={}
for k, v in pairs(a) do
b[k] = v
end
return b
end
function bubble_sort(a)
for i=#a, 2, -1 do
local sorted = true
for j=1, (i-1), 1 do
if a[j+1] < a[j] then
a[j+1],a[j] = a[j],a[j+1]
sorted = false
end
end
if sorted then
break
end
end
end
function partition(a, first, last, pivot)
a[pivot],a[last] = a[last],a[pivot]
j = first
for i=first, last-1, 1 do
if a[i] <= a[last] then
a[i], a[j] = a[j], a[i]
j = j + 1
end
end
a[last], a[j] = a[j], a[last]
return j
end
function quick_sort(a)
local first = 1
local last = #a
stack = {}
table.insert(stack, first)
table.insert(stack, last)
while #stack>0 do
last = table.remove(stack, #stack)
first = table.remove(stack, #stack)
if first < last then
pivot = last
pivot = partition(a, first, last, pivot)
table.insert(stack, first)
table.insert(stack, pivot-1)
table.insert(stack, pivot+1)
table.insert(stack, last)
end
end
end
-- ---------------------------------------------- main
local my_bench_data={}
local algos={bubble = bubble_sort, quick = quick_sort}
for algo, sorter in pairs(algos) do
for n, data_n in pairs(data) do
local a=array_copy(data_n["random"])
local b=array_copy(data_n["reverse"])
local c=array_copy(data_n["nearly"])
t0=os.clock()
sorter(a)
sorter(b)
sorter(c)
dt=os.clock() - t0
print(string.format("%-8s %-14s %6d %8.3f", LANGUAGE, algo.." sort", n, dt))
table.insert(my_bench_data, {machine=MACHINE, language=LANGUAGE, algo=algo, n=n, t=dt})
end
end
local f = assert(io.open("bench_data.json", "rb"))
local content = f:read("*all")
f:close()
local bench_data = json.decode(content)
for _,v in ipairs(my_bench_data) do
table.insert(bench_data, v)
end
local f = assert(io.open("bench_data.json", "w+"))
f:write(json.encode(bench_data))
f:close()