Skip to content

Commit 1244068

Browse files
author
Shawn Bragdon
committed
Create Lua sorting algorithms.
1 parent a060ab6 commit 1244068

File tree

16 files changed

+448
-0
lines changed

16 files changed

+448
-0
lines changed

sortingAlgo/bubbleSort/bubbleSort.lua

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local function bubbleSort(t)
2+
local n = #t
3+
for i = 1, n do
4+
for j = 1, n - i do
5+
if t[j] > t[j + 1] then
6+
t[j], t[j + 1] = t[j + 1], t[j]
7+
end
8+
end
9+
end
10+
end
11+
12+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
13+
bubbleSort(t)
14+
print(table.concat(t, ", "))

sortingAlgo/bucketsort/bucketSort.lua

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local function bucketSort(t)
2+
local n = #t
3+
local buckets = {}
4+
for i = 1, n do
5+
local bucket = math.floor(n * t[i])
6+
buckets[bucket] = buckets[bucket] or {}
7+
table.insert(buckets[bucket], t[i])
8+
end
9+
for i = 1, n do
10+
if buckets[i] then
11+
table.sort(buckets[i])
12+
end
13+
end
14+
local k = 1
15+
for i = 1, n do
16+
if buckets[i] then
17+
for j = 1, #buckets[i] do
18+
t[k] = buckets[i][j]
19+
k = k + 1
20+
end
21+
end
22+
end
23+
end
24+
25+
local t = { 0.3, 0.2, 0.1, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 }
26+
bucketSort(t)
27+
print(table.concat(t, ", "))
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local function cocktailSort(t)
2+
local n = #t
3+
local swapped = true
4+
local start = 1
5+
local end_ = n - 1
6+
while swapped do
7+
swapped = false
8+
for i = start, end_ do
9+
if t[i] > t[i + 1] then
10+
t[i], t[i + 1] = t[i + 1], t[i]
11+
swapped = true
12+
end
13+
end
14+
if not swapped then
15+
break
16+
end
17+
swapped = false
18+
end_ = end_ - 1
19+
for i = end_, start, -1 do
20+
if t[i] < t[i - 1] then
21+
t[i], t[i - 1] = t[i - 1], t[i]
22+
swapped = true
23+
end
24+
end
25+
start = start + 1
26+
end
27+
end
28+
29+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
30+
cocktailSort(t)
31+
print(table.concat(t, ", "))

sortingAlgo/combSort/combSort.lua

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
local function combSort(t)
2+
local n = #t
3+
local gap = n
4+
local shrink = 1.3
5+
local swapped = true
6+
while gap > 1 or swapped do
7+
gap = math.floor(gap / shrink)
8+
if gap < 1 then
9+
gap = 1
10+
end
11+
swapped = false
12+
for i = 1, n - gap do
13+
if t[i] > t[i + gap] then
14+
t[i], t[i + gap] = t[i + gap], t[i]
15+
swapped = true
16+
end
17+
end
18+
end
19+
end
20+
21+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
22+
combSort(t)
23+
print(table.concat(t, ", "))
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local function countingSort(t)
2+
local n = #t
3+
local counts = {}
4+
for i = 1, n do
5+
counts[t[i]] = (counts[t[i]] or 0) + 1
6+
end
7+
local k = 1
8+
for i = 1, n do
9+
if counts[i] then
10+
for j = 1, counts[i] do
11+
t[k] = i
12+
k = k + 1
13+
end
14+
end
15+
end
16+
end
17+
18+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
19+
countingSort(t)
20+
print(table.concat(t, ", "))

sortingAlgo/cycleSort/cycleSort.lua

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
local function cycleSort(t)
2+
local n = #t
3+
for i = 1, n do
4+
local item = t[i]
5+
local pos = i
6+
for j = i + 1, n do
7+
if t[j] < item then
8+
pos = pos + 1
9+
end
10+
end
11+
if pos == i then
12+
goto continue
13+
end
14+
while item == t[pos] do
15+
pos = pos + 1
16+
end
17+
t[pos], item = item, t[pos]
18+
::continue::
19+
while pos ~= i do
20+
pos = i
21+
for j = i + 1, n do
22+
if t[j] < item then
23+
pos = pos + 1
24+
end
25+
end
26+
while item == t[pos] do
27+
pos = pos + 1
28+
end
29+
t[pos], item = item, t[pos]
30+
end
31+
end
32+
end
33+
34+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
35+
cycleSort(t)
36+
print(table.concat(t, ", "))

sortingAlgo/heapSort/heapSort.lua

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local function heapSort(t)
2+
local n = #t
3+
local function heapify(t, n, i)
4+
local largest = i
5+
local l = 2 * i
6+
local r = 2 * i + 1
7+
if l <= n and t[l] > t[largest] then
8+
largest = l
9+
end
10+
if r <= n and t[r] > t[largest] then
11+
largest = r
12+
end
13+
if largest ~= i then
14+
t[i], t[largest] = t[largest], t[i]
15+
heapify(t, n, largest)
16+
end
17+
end
18+
for i = math.floor(n / 2), 1, -1 do
19+
heapify(t, n, i)
20+
end
21+
for i = n, 2, -1 do
22+
t[1], t[i] = t[i], t[1]
23+
heapify(t, i - 1, 1)
24+
end
25+
end
26+
27+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
28+
heapSort(t)
29+
print(table.concat(t, ", "))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local function insertionSort(t)
2+
local n = #t
3+
for i = 2, n do
4+
local item = t[i]
5+
local j = i - 1
6+
while j > 0 and t[j] > item do
7+
t[j + 1] = t[j]
8+
j = j - 1
9+
end
10+
t[j + 1] = item
11+
end
12+
end
13+
14+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
15+
insertionSort(t)
16+
print(table.concat(t, ", "))

sortingAlgo/mergeSort/mergeSort.lua

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local function mergeSort(t)
2+
local n = #t
3+
if n < 2 then
4+
return t
5+
end
6+
local mid = math.floor(n / 2)
7+
local left = {}
8+
local right = {}
9+
for i = 1, mid do
10+
left[i] = t[i]
11+
end
12+
for i = mid + 1, n do
13+
right[i - mid] = t[i]
14+
end
15+
left = mergeSort(left)
16+
right = mergeSort(right)
17+
local i = 1
18+
local j = 1
19+
local k = 1
20+
while i <= #left and j <= #right do
21+
if left[i] < right[j] then
22+
t[k] = left[i]
23+
i = i + 1
24+
else
25+
t[k] = right[j]
26+
j = j + 1
27+
end
28+
k = k + 1
29+
end
30+
while i <= #left do
31+
t[k] = left[i]
32+
i = i + 1
33+
k = k + 1
34+
end
35+
while j <= #right do
36+
t[k] = right[j]
37+
j = j + 1
38+
k = k + 1
39+
end
40+
return t
41+
end
42+
43+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
44+
mergeSort(t)
45+
print(table.concat(t, ", "))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local function pigeonholeSort(t)
2+
local n = #t
3+
local min = math.huge
4+
local max = -math.huge
5+
for i = 1, n do
6+
if t[i] < min then
7+
min = t[i]
8+
end
9+
if t[i] > max then
10+
max = t[i]
11+
end
12+
end
13+
local holes = {}
14+
for i = 1, n do
15+
holes[i] = 0
16+
end
17+
for i = 1, n do
18+
holes[t[i] - min + 1] = holes[t[i] - min + 1] + 1
19+
end
20+
local k = 1
21+
for i = 1, n do
22+
while holes[i] > 0 do
23+
t[k] = i + min - 1
24+
k = k + 1
25+
holes[i] = holes[i] - 1
26+
end
27+
end
28+
end
29+
30+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
31+
pigeonholeSort(t)
32+
print(table.concat(t, ", "))

sortingAlgo/quickSort/quickSort.lua

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local function quickSort(t)
2+
local n = #t
3+
local function sort(t, start, end_)
4+
if start < end_ then
5+
local pivot = t[end_]
6+
local i = start - 1
7+
for j = start, end_ - 1 do
8+
if t[j] <= pivot then
9+
i = i + 1
10+
t[i], t[j] = t[j], t[i]
11+
end
12+
end
13+
t[i + 1], t[end_] = t[end_], t[i + 1]
14+
local p = i + 1
15+
sort(t, start, p - 1)
16+
sort(t, p + 1, end_)
17+
end
18+
end
19+
sort(t, 1, n)
20+
end
21+
22+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
23+
quickSort(t)
24+
print(table.concat(t, ", "))

sortingAlgo/radixSort/radixSort.lua

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
local function radixSort(t)
2+
local n = #t
3+
local max = 0
4+
for i = 1, n do
5+
if t[i] > max then
6+
max = t[i]
7+
end
8+
end
9+
local exp = 1
10+
while max / exp > 0 do
11+
local counts = {}
12+
for i = 1, n do
13+
local digit = math.floor(t[i] / exp) % 10
14+
counts[digit] = (counts[digit] or 0) + 1
15+
end
16+
for i = 1, 9 do
17+
counts[i] = (counts[i] or 0) + (counts[i - 1] or 0)
18+
end
19+
local sorted = {}
20+
for i = n, 1, -1 do
21+
local digit = math.floor(t[i] / exp) % 10
22+
sorted[counts[digit]] = t[i]
23+
counts[digit] = counts[digit] - 1
24+
end
25+
for i = 1, n do
26+
t[i] = sorted[i]
27+
end
28+
exp = exp * 10
29+
end
30+
end
31+
32+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
33+
radixSort(t)
34+
print(table.concat(t, ", "))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local function selectionSort(t)
2+
local n = #t
3+
for i = 1, n - 1 do
4+
local min = i
5+
for j = i + 1, n do
6+
if t[j] < t[min] then
7+
min = j
8+
end
9+
end
10+
t[i], t[min] = t[min], t[i]
11+
end
12+
end
13+
14+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
15+
selectionSort(t)
16+
print(table.concat(t, ", "))

sortingAlgo/shellSort/shellSort.lua

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local function shellSort(t)
2+
local n = #t
3+
local gap = math.floor(n / 2)
4+
while gap > 0 do
5+
for i = gap + 1, n do
6+
local item = t[i]
7+
local j = i - gap
8+
while j > 0 and t[j] > item do
9+
t[j + gap] = t[j]
10+
j = j - gap
11+
end
12+
t[j + gap] = item
13+
end
14+
gap = math.floor(gap / 2)
15+
end
16+
end
17+
18+
local t = { 3, 2, 1, 4, 5, 6, 7, 8, 9, 10 }
19+
shellSort(t)
20+
print(table.concat(t, ", "))
File renamed without changes.

0 commit comments

Comments
 (0)