Advertisement
eniallator

Sorting Algorithm | Lua

Nov 15th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.62 KB | None | 0 0
  1. local function sort(inTbl, ascending, start, finish)
  2.     start, finish = start or 1, finish or #inTbl
  3.     for i = start, finish do
  4.         for j = start, finish - i do
  5.             if ascending and inTbl[j] > inTbl[j + 1] or not ascending and inTbl[j] < inTbl[j + 1] then
  6.                 inTbl[j], inTbl[j + 1] = inTbl[j + 1], inTbl[j]
  7.             end
  8.         end
  9.     end
  10. end
  11.  
  12. local tbl = {2, 6, 34, 8, 4, 5, 1, 13, 68, 9, 12, 23, 7, 3}
  13. sort(tbl, true, 3, 10)
  14.  
  15. -- Turning it into a string and printing it
  16.  
  17. local s = ''
  18. for _, val in pairs(tbl) do
  19.     s = s .. (s == '' and '' or ', ') .. val
  20. end
  21.  
  22. print('{' .. s .. '}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement