Skip to content

Instantly share code, notes, and snippets.

@haruki7049
Created May 29, 2025 11:40
Show Gist options
  • Save haruki7049/4707c43d71e07140040e470428c7ea07 to your computer and use it in GitHub Desktop.
Save haruki7049/4707c43d71e07140040e470428c7ea07 to your computer and use it in GitHub Desktop.
-- Original data
data = {2, 3, 55, 22, 1, 4, 5, 33, 44, 11}
-- Debugging function
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function bubble(data, index)
target = data[index]
next_target = data[index+1]
if target > next_target then
temp = next_target
next_target = target
target = temp
end
print("Table: ", dump(data))
data[index] = target
data[index+1] = next_target
end
for i=1, #data-1, 1 do
for i=1, #data-1, 1 do
bubble(data, i)
end
end
-- Output
for i=1, #data, 1 do
print(data[i])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment