Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Sort an array using insertion sort.
- -- @param array The input array
- -- @return The sorted array
- local function insertion_sort( array )
- for i = 1, #array do
- local value = array[ i ]
- while i > 1 and array[ i - 1 ] > value do
- -- swap
- array[ i - 1 ], array[ i ] = array[ i ], array[ i - 1 ]
- i = i - 1
- end
- end
- return array
- end
- --- Sort an array using selection sort.
- -- @param array The input array
- -- @return The sorted array
- local function selection_sort( array )
- local len = #array
- for i = 1, len do
- -- find the lowest value
- local best = i
- for j = i + 1, len do
- if array[ j ] < array[ best ] then
- best = j
- end
- end
- -- swap with the current position
- array[ best ], array[ i ] = array[ i ], array[ best ]
- end
- return array
- end
- --- Sort an array using bubble sort.
- -- @param array The input array
- -- @return The sorted array
- local function bubble_sort( array )
- repeat
- local swapped = false
- for i = 1, #array - 1 do
- if array[ i ] > array[ i + 1 ] then
- array[ i + 1 ], array[ i ] = array[ i ], array[ i + 1 ]
- swapped = true
- end
- end
- until not swapped
- return array
- end
- --- Clone an array.
- -- @param array The input array
- -- @return The new array
- local function copy_array( array )
- local result = {}
- for i, v in ipairs( array ) do
- result[ i ] = v
- end
- return result
- end
- --- Compare two arrays for equality.
- -- @param a One of the arrays used for the comparison
- -- @param b One of the arrays used for the comparison
- -- @return true or false
- local function compare_arrays( a, b )
- -- smoke test
- if #a ~= #b then
- return false
- end
- for i = 1, #a do
- if a[ i ] ~= b[ i ] then
- return false
- end
- end
- return true
- end
- local input = {
- { 1 };
- { 1, 2, 4, 3 };
- { 5, 1, 4, 2, 8 };
- { 3, 7, 4, 9, 5, 2, 6, 1 };
- }
- local correct = {
- { 1 };
- { 1, 2, 3, 4 };
- { 1, 2, 4, 5, 8 };
- { 1, 2, 3, 4, 5, 6, 7, 9 };
- }
- --- Test a sort algorithm.
- -- @return nil
- local function test( name, fn )
- print( "Testing " .. name )
- local passed = 0
- for i = 1, #input do
- local result = fn( copy_array( input[ i ] ) )
- if compare_arrays( result, correct[ i ] ) then
- passed = passed + 1
- print( " passed" )
- else
- print( " !failed" )
- print( " expected result:" )
- print( " { " .. table.concat( correct[ i ], ", " ) .. " }" )
- print( " diverging result:" )
- if type( result ) == "table" then
- print( " { " .. table.concat( result, ", " ) .. " }" )
- else
- print( " a " .. type( result ) .. " value of '" .. tostring( result ) .. "'" )
- end
- end
- end
- print( " passed " .. passed .. "/" .. #input )
- local fails = #input - passed
- if fails > 0 then
- print( "WARN: " .. fails .. " test failures!" )
- end
- print()
- end
- -- Tests
- test( "insertion sort", insertion_sort )
- test( "selection sort", selection_sort )
- test( "bubble sort", bubble_sort )
Advertisement
Add Comment
Please, Sign In to add comment