-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.lua
More file actions
91 lines (71 loc) · 1.63 KB
/
quicksort.lua
File metadata and controls
91 lines (71 loc) · 1.63 KB
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
-- Wandelt Zahl von command line uin String um
local number_ = arg[1]
local number = tostring(number_)
--print(type(number))
-- Zähler und zu sortierender table
local count = 1
local sort_table = {}
-- Fügt den char an Indexposition als eigenes Element dem table hinzu
for i = 1, #number do
table.insert(sort_table, string.sub(number, i, i))
--print(string.sub(number, i, i))
count = count + 1
end
--#region quicksort
--local function confirm_sort(tbl)
-- local sorted = true
-- local i = 1
-- local cap = #tbl - 1
--
-- while not sorted and (i <= cap) do
-- sorted = (tbl[i] <= tbl[i + 1])
-- i = i + 1
-- end
--
-- return sorted
--end
local function table_concat(t1, t2)
local res = {}
for i = 1, #t1 do
res[#res + 1] = t1[i]
end
for i = 1, #t2 do
res[#res + 1] = t2[i]
end
return res
end
local function sort(tbl)
if #tbl <= 1 then
return tbl
end
local i = 0
local pivot = tbl[#tbl] -- letzter char als pivot-Element
for j = 1, (#tbl - 1) do
if tbl[j] < pivot then
i = i + 1
local temp = tbl[i]
tbl[i] = tbl[j]
tbl[j] = temp
end
end
local temp = tbl[i + 1]
tbl[i + 1] = pivot
tbl[#tbl] = temp
local left_part = {}
local right_aprt = {}
for k = 1, i do
left_part[k] = tbl[k]
end
for k = i + 2, #tbl do
right_aprt[#right_aprt + 1] = tbl[k]
end
local sorted_left = sort(left_part)
local sorted_right = sort(right_aprt)
local res = table_concat(sorted_left, {tbl[i + 1]})
res = table_concat(res, sorted_right)
return res
end
print("Unsorted:\t" .. table.concat(sort_table, ", "))
local sorted_list = sort(sort_table)
print("Sorted:\t\t" .. table.concat(sorted_list, ", "))
--#endregion quicksort