Modul:Mergesort

Aus KGS-Wiki

Die Dokumentation für dieses Modul kann unter Modul:Mergesort/Doku erstellt werden

local p = {}

function createRandomList()
	math.randomseed( tonumber( mw.getContentLanguage():formatDate( "U" ) ) * 10000 + os.clock() * 10000 )
	local res = {}
	for i = 1,8 do
		res[i] = math.random(99)
	end
	return res
end

function sublist(l,from,to)
	local res = {}
	local index = 1
	for i = from,to do
		res[index] = l[i]
		index = index + 1
	end
	return res
end

function p.example()
	local l = createRandomList()
	local l14 = sublist(l,1,4)
	local l12 = sublist(l,1,2)
	local l34 = sublist(l,3,4)
	local l58 = sublist(l,5,8)
	local l56 = sublist(l,5,6)
	local l78 = sublist(l,7,8)
	frame = mw.getCurrentFrame()
	local content = 'graph TD\n'
	content = content .. 'subgraph divide\n'
	content = content .. 'a[' .. table.concat(l,' ') .. ']\n'
	content = content .. 'a --> b[' .. table.concat(l14,' ') .. '] & c[' .. table.concat(l58,' ') .. ']\n'
	content = content .. 'b --> d[' .. table.concat(l12,' ') .. '] & e[' .. table.concat(l34,' ') .. ']\n'
	content = content .. 'c --> f[' .. table.concat(l56,' ') .. '] & g[' .. table.concat(l78,' ') .. ']\n'
	content = content .. 'd --> h[' .. l[1] .. '] & i[' .. l[2] .. ']\n'
	content = content .. 'e --> j[' .. l[3] .. '] & k[' .. l[4] .. ']\n'
	content = content .. 'f --> l[' .. l[5] .. '] & m[' .. l[6] .. ']\n'
	content = content .. 'g --> n[' .. l[7] .. '] & o[' .. l[8] .. ']\n'
	content = content .. 'end\n'
	table.sort(l)
	table.sort(l12)
	table.sort(l34)
	table.sort(l56)
	table.sort(l78)
	table.sort(l14)
	table.sort(l58)
	content = content .. 'subgraph conquer\n'
	content = content .. 'h --> p[' .. table.concat(l12,' ') .. ']\n'
	content = content .. 'i --> p\n'
	content = content .. 'j --> q[' .. table.concat(l34,' ') .. ']\n'
	content = content .. 'k --> q\n'
    content = content .. 'l --> r[' .. table.concat(l56,' ') .. ']\n'
	content = content .. 'm --> r\n'
	content = content .. 'n --> s[' .. table.concat(l78,' ') .. ']\n'
	content = content .. 'o --> s\n'
	content = content .. 'p --> t[' .. table.concat(l14,' ') .. ']\n'
	content = content .. 'q --> t\n'
	content = content .. 'r --> u[' .. table.concat(l58,' ') .. ']\n'
	content = content .. 's --> u\n'
	content = content .. 't --> v[' .. table.concat(l,' ') .. ']\n'
	content = content .. 'u --> v\n'
	content = content .. 'end'
local graph = frame:extensionTag('mermaid', content, {})
return graph
end

return p