Skip to content

Commit 4ab5b4a

Browse files
GorzoidKen Power
authored and
Ken Power
committed
Bubble sort lua (algorithm-archivists#412)
* Added Bubble Sort in Lua * Added test case Stole numbers from C code /: * Moved files cus derp * Added Lua entry to md file Pretty sure I got it right, should probably run the book to check * Use tuple style swap Forgot lua could do this derp. * Addressing problems Fixed bubble_sort.md and small changes to code * Removed unnecessary line specification Not necessary when importing entire file * Swap code broken Probably should have tested before pushing
1 parent a33a634 commit 4ab5b4a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

contents/bubble_sort/bubble_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
2222
[import:4-9, lang:"python"](code/python/bubblesort.py)
2323
{% sample lang="m" %}
2424
[import:1-13, lang:"matlab"](code/matlab/bubblesort.m)
25+
{% sample lang="lua" %}
26+
[import:1-9, lang="lua"](code/lua/bubble_sort.lua)
2527
{% sample lang="hs" %}
2628
[import, lang:"haskell"](code/haskell/bubbleSort.hs)
2729
{% sample lang="cpp" %}
@@ -79,6 +81,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the
7981
[import, lang:"python"](code/python/bubblesort.py)
8082
{% sample lang="m" %}
8183
[import, lang:"matlab"](code/matlab/bubblesort.m)
84+
{% sample lang="lua" %}
85+
[import, lang="lua"](code/lua/bubble_sort.lua)
8286
{% sample lang="hs" %}
8387
[import, lang:"haskell"](code/haskell/bubbleSort.hs)
8488
{% sample lang="cpp" %}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function bubble_sort(arr)
2+
for i = 1,#arr-1 do
3+
for j = 1,#arr-1 do
4+
if arr[j] > arr[j+1] then
5+
arr[j], arr[j+1] = arr[j+1], arr[j]
6+
end
7+
end
8+
end
9+
end
10+
11+
local arr = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4}
12+
print(("Unsorted array: {%s}"):format(table.concat(arr,", ")))
13+
14+
bubble_sort(arr)
15+
16+
print(("Sorted array: {%s}"):format(table.concat(arr,", ")))

0 commit comments

Comments
 (0)