## bubble sort

### Data

in lua

#### Tags

exchange sort, sorting

### Source Code

``````--[[
Author: Duc Thanh Tran (ducthanhtran, Github)
Algorithm: Bubblesort, slightly optimized version
Date: 12/31/2013
--]]

local function bubblesort(T)
assert(type(T) == "table", "Argument is not a table")
local length = #T
local changed

repeat
changed = false
length = length - 1

for i = 1, length do
if T[i] > T[i+1] then
T[i], T[i+1] = T[i+1], T[i]
changed = true
end
end
until changed == false
end

return bubblesort
``````
``````--[[
Author: Duc Thanh Tran (ducthanhtran, Github)
Algorithm: Bubblesort, slightly optimized version
Date: 12/31/2013
--]]

local bubblesort = require "bubblesort"

array = {3, -1, 5, 9, 0, 10, 4, 2}

print("Unsorted list:")
print(table.concat(array, ", "))

bubblesort(array)

print("\nSorted list:")
print(table.concat(array, ", "))

assert(array[1] == -1, "Failed")
assert(array[2] == 0, "Failed")
assert(array[3] == 2, "Failed")
assert(array[4] == 3, "Failed")
assert(array[5] == 4, "Failed")
assert(array[6] == 5, "Failed")
assert(array[7] == 9, "Failed")
assert(array[8] == 10, "Failed")
``````