Skip to content

Instantly share code, notes, and snippets.

@baiyanhuang
Created June 21, 2011 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baiyanhuang/1037031 to your computer and use it in GitHub Desktop.
Save baiyanhuang/1037031 to your computer and use it in GitHub Desktop.
--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