Created
June 21, 2011 01:21
-
-
Save hpcx82/1037031 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
--Bubble Sort | |
-- 1, 3, 2, 7 | |
function bubble_sort(arr) | |
for i=table.getn(arr), 2, -1 do -- each loop put one element in right position, decrease! | |
for j=2, i do -- inside the loop, do the exchange one by one | |
if arr[j] < arr[j-1] then | |
tmp = arr[j-1] | |
arr[j-1] = arr[j] | |
arr[j] = tmp | |
end | |
end | |
end | |
return arr | |
end | |
res = bubble_sort{4, 3, 2, 5, 8, 1} | |
for k in pairs(res) do print(k) end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment