diff --git a/contents/bubble_sort/bubble_sort.md b/contents/bubble_sort/bubble_sort.md index f18fb0e7a..2bdfc995a 100644 --- a/contents/bubble_sort/bubble_sort.md +++ b/contents/bubble_sort/bubble_sort.md @@ -22,6 +22,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with [import:4-9, lang:"python"](code/python/bubblesort.py) {% sample lang="m" %} [import:1-13, lang:"matlab"](code/matlab/bubblesort.m) +{% sample lang="lua" %} +[import:1-9, lang="lua"](code/lua/bubble_sort.lua) {% sample lang="hs" %} [import, lang:"haskell"](code/haskell/bubbleSort.hs) {% sample lang="cpp" %} @@ -77,6 +79,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the [import, lang:"python"](code/python/bubblesort.py) {% sample lang="m" %} [import, lang:"matlab"](code/matlab/bubblesort.m) +{% sample lang="lua" %} +[import, lang="lua"](code/lua/bubble_sort.lua) {% sample lang="hs" %} [import, lang:"haskell"](code/haskell/bubbleSort.hs) {% sample lang="cpp" %} diff --git a/contents/bubble_sort/code/lua/bubble_sort.lua b/contents/bubble_sort/code/lua/bubble_sort.lua new file mode 100644 index 000000000..d485fb036 --- /dev/null +++ b/contents/bubble_sort/code/lua/bubble_sort.lua @@ -0,0 +1,16 @@ +function bubble_sort(arr) + for i = 1,#arr-1 do + for j = 1,#arr-1 do + if arr[j] > arr[j+1] then + arr[j], arr[j+1] = arr[j+1], arr[j] + end + end + end +end + +local arr = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4} +print(("Unsorted array: {%s}"):format(table.concat(arr,", "))) + +bubble_sort(arr) + +print(("Sorted array: {%s}"):format(table.concat(arr,", ")))