Created
May 29, 2025 11:40
-
-
Save haruki7049/4707c43d71e07140040e470428c7ea07 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| -- 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