Andy73

sort.lua

May 12th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1.  
  2. --- Sort an array using insertion sort.
  3. -- @param array The input array
  4. -- @return The sorted array
  5. local function insertion_sort( array )
  6.     for i = 1, #array do
  7.         local value = array[ i ]
  8.  
  9.         while i > 1 and array[ i - 1 ] > value do
  10.             -- swap
  11.             array[ i - 1 ], array[ i ] = array[ i ], array[ i - 1 ]
  12.  
  13.             i = i - 1
  14.         end
  15.     end
  16.  
  17.     return array
  18. end
  19.  
  20. --- Sort an array using selection sort.
  21. -- @param array The input array
  22. -- @return The sorted array
  23. local function selection_sort( array )
  24.     local len = #array
  25.  
  26.     for i = 1, len do
  27.         -- find the lowest value
  28.         local best = i
  29.         for j = i + 1, len do
  30.             if array[ j ] < array[ best ] then
  31.                 best = j
  32.             end
  33.         end
  34.  
  35.         -- swap with the current position
  36.         array[ best ], array[ i ] = array[ i ], array[ best ]
  37.     end
  38.  
  39.     return array
  40. end
  41.  
  42. --- Sort an array using bubble sort.
  43. -- @param array The input array
  44. -- @return The sorted array
  45. local function bubble_sort( array )
  46.     repeat
  47.         local swapped = false
  48.  
  49.         for i = 1, #array - 1 do
  50.             if array[ i ] > array[ i + 1 ] then
  51.                 array[ i + 1 ], array[ i ] = array[ i ], array[ i + 1 ]
  52.                 swapped = true
  53.             end
  54.         end
  55.     until not swapped
  56.  
  57.     return array
  58. end
  59.  
  60. --- Clone an array.
  61. -- @param array The input array
  62. -- @return The new array
  63. local function copy_array( array )
  64.     local result = {}
  65.  
  66.     for i, v in ipairs( array ) do
  67.         result[ i ] = v
  68.     end
  69.  
  70.     return result
  71. end
  72.  
  73. --- Compare two arrays for equality.
  74. -- @param a One of the arrays used for the comparison
  75. -- @param b One of the arrays used for the comparison
  76. -- @return true or false
  77. local function compare_arrays( a, b )
  78.     -- smoke test
  79.     if #a ~= #b then
  80.         return false
  81.     end
  82.  
  83.     for i = 1, #a do
  84.         if a[ i ] ~= b[ i ] then
  85.             return false
  86.         end
  87.     end
  88.  
  89.     return true
  90. end
  91.  
  92. local input   = {
  93.     { 1 };
  94.     { 1, 2, 4, 3 };
  95.     { 5, 1, 4, 2, 8 };
  96.     { 3, 7, 4, 9, 5, 2, 6, 1 };
  97. }
  98.  
  99. local correct = {
  100.     { 1 };
  101.     { 1, 2, 3, 4 };
  102.     { 1, 2, 4, 5, 8 };
  103.     { 1, 2, 3, 4, 5, 6, 7, 9 };
  104. }
  105.  
  106. --- Test a sort algorithm.
  107. -- @return nil
  108. local function test( name, fn )
  109.     print( "Testing " .. name )
  110.  
  111.     local passed = 0
  112.  
  113.     for i = 1, #input do
  114.         local result = fn( copy_array( input[ i ] ) )
  115.  
  116.         if compare_arrays( result, correct[ i ] ) then
  117.             passed = passed + 1
  118.             print( "   passed" )
  119.         else
  120.             print( "  !failed" )
  121.  
  122.             print( "     expected result:" )
  123.             print( "     { " .. table.concat( correct[ i ], ", " ) .. " }" )
  124.  
  125.             print( "     diverging result:" )
  126.             if type( result ) == "table" then
  127.                 print( "     { " .. table.concat( result,   ", " ) .. " }" )
  128.             else
  129.                 print( "     a " .. type( result ) .. " value of '" .. tostring( result ) .. "'" )
  130.             end
  131.         end
  132.     end
  133.  
  134.     print( "  passed " .. passed .. "/" .. #input )
  135.  
  136.     local fails = #input - passed
  137.  
  138.     if fails > 0 then
  139.         print( "WARN: " .. fails .. " test failures!" )
  140.     end
  141.  
  142.     print()
  143. end
  144.  
  145. -- Tests
  146.  
  147. test( "insertion sort", insertion_sort )
  148. test( "selection sort", selection_sort )
  149. test( "bubble sort",    bubble_sort )
Advertisement
Add Comment
Please, Sign In to add comment