Skip to content

Instantly share code, notes, and snippets.

View ladc's full-sized avatar

Lesley De Cruz ladc

  • Royal Meteorological Institute
  • Belgium
View GitHub Profile
-- This is the LuaJIT implementation of Smoothsort [1], a comparison-based
-- sorting algorithm with worst-case asymptotic O(n log n) behaviour, best-case
-- O(n) behaviour, and a smooth transition in between. Largely based on the C++
-- code by Keith Schwarz [2], translated to LuaJIT by Lesley De Cruz.
-- [1] Dijkstra, E. W. (1982). Smoothsort, an alternative for sorting in situ.
-- Science of Computer Programming, 1(3), 223-233.
-- [2] Schwarz, K. Smoothsort demystified. http://www.keithschwarz.com/smoothsort/.
local ffi = require("ffi")
local v1 = 1-5e-9
local v2 = 1-5e-9-1e-16
local function sprintf(...)
io.write(string.format(...))
end
print("13 hex digits define a double, so these numbers are almost identical:")
sprintf("%.13a\n",v1)
sprintf("%.13a\n",v2)
print("However their \"%.8g\" representation is still different:")