Last active
August 29, 2015 14:05
-
-
Save hoffoo/b420d1ecc60ad6ec44e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local array = {1,2,0,0,3,1,2} | |
function swap(a, b, table) | |
if table[a] == nil or table[b] == nil then | |
return false | |
end | |
if table[a] > table[b] then | |
table[a], table[b] = table[b], table[a] | |
return true | |
end | |
return false | |
end | |
function bubblesort(array) | |
for i=1,table.maxn(array) do | |
local ci = i | |
::redo:: | |
if swap(ci, ci+1, array) then | |
ci = ci - 1 | |
goto redo | |
end | |
end | |
end | |
bubblesort(array) | |
for k, v in pairs(array) do | |
print(k, " ", v) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting implementation... are goto's more efficient than recursion?