Modul:Mergesort: Unterschied zwischen den Versionen

Aus KGS-Wiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
(12 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 2: Zeile 2:


function createRandomList()
function createRandomList()
math.randomseed( tonumber( mw.getContentLanguage():formatDate( "U" ) ) * 10000 + os.clock() * 10000 )
local res = {}
local res = {}
for i in 1,8,1 do
for i = 1,8 do
res[i] = math.random(99)
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
end
return res
return res
Zeile 11: Zeile 22:
function p.example()
function p.example()
local l = createRandomList()
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()
frame = mw.getCurrentFrame()
local content = 'graph TD\n'
local content = 'graph TD\n'
content = content .. 'subgraph divide\n'
content = content .. 'subgraph divide\n'
content = content .. 'a[' .. unpack(l) .. ']\n'
content = content .. 'a[' .. table.concat(l,' ') .. ']\n'
--content = content .. 'a --> b[' .. a .. ' ' .. b .. ' ' .. c .. ' ' .. d .. '] & c[' .. e .. ' ' .. f .. ' ' .. g .. ' ' .. h .. ']\n'
content = content .. 'a --> b[' .. table.concat(l14,' ') .. '] & c[' .. table.concat(l58,' ') .. ']\n'
--content = content .. 'b --> d[' .. a .. ' ' .. b .. '] & e[' .. c .. ' ' .. d .. ']\n'
content = content .. 'b --> d[' .. table.concat(l12,' ') .. '] & e[' .. table.concat(l34,' ') .. ']\n'
--content = content .. 'c --> f[' .. e .. ' ' .. f .. '] & g[' .. g .. ' ' .. h .. ']\n'
content = content .. 'c --> f[' .. table.concat(l56,' ') .. '] & g[' .. table.concat(l78,' ') .. ']\n'
--content = content .. 'd --> h[' .. a .. '] & i[' .. b .. ']\n'
content = content .. 'd --> h[' .. l[1] .. '] & i[' .. l[2] .. ']\n'
--content = content .. 'e --> j[' .. c .. '] & k[' .. d .. ']\n'
content = content .. 'e --> j[' .. l[3] .. '] & k[' .. l[4] .. ']\n'
--content = content .. 'f --> l[' .. e .. '] & m[' .. f .. ']\n'
content = content .. 'f --> l[' .. l[5] .. '] & m[' .. l[6] .. ']\n'
--content = content .. 'g --> n[' .. g .. '] & o[' .. h .. ']\n'
content = content .. 'g --> n[' .. l[7] .. '] & o[' .. l[8] .. ']\n'
content = content .. 'end\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 .. 'subgraph conquer\n'
--h --> p["{{#af_join:{{#af_sort:{{#af_list:{{{1|94}}} | {{{2|73}}}}}}}| }}"]
content = content .. 'h --> p[' .. table.concat(l12,' ') .. ']\n'
--i --> p
content = content .. 'i --> p\n'
--j --> q["{{#af_join:{{#af_sort:{{#af_list:{{{3|58}}} | {{{4|61}}}}}}}| }}"]
content = content .. 'j --> q[' .. table.concat(l34,' ') .. ']\n'
--k --> q
content = content .. 'k --> q\n'
--l --> r["{{#af_join:{{#af_sort:{{#af_list:{{{5|22}}} | {{{6|20}}}}}}}| }}"]
    content = content .. 'l --> r[' .. table.concat(l56,' ') .. ']\n'
--m --> r
content = content .. 'm --> r\n'
--n --> s["{{#af_join:{{#af_sort:{{#af_list:{{{7|46}}} | {{{8|75}}}}}}}| }}"]
content = content .. 'n --> s[' .. table.concat(l78,' ') .. ']\n'
--o --> s
content = content .. 'o --> s\n'
--p --> t["{{#af_join:{{#af_sort:{{#af_list:{{{3|58}}} | {{{4|61}}} | {{{2|73}}} | {{{1|94}}}}}}}| }}"]
content = content .. 'p --> t[' .. table.concat(l14,' ') .. ']\n'
--q --> t
content = content .. 'q --> t\n'
--r --> u["{{#af_join:{{#af_sort:{{#af_list:{{{6|20}}} | {{{5|22}}} | {{{7|46}}} | {{{8|75}}}}}}}| }}"]
content = content .. 'r --> u[' .. table.concat(l58,' ') .. ']\n'
--s --> u
content = content .. 's --> u\n'
--t --> v["{{#af_join:{{#af_sort:{{#af_list:{{{6|20}}} | {{{5|22}}} | {{{7|46}}} | {{{3|58}}} | {{{4|61}}} | {{{2|73}}} | {{{8|75}}} | {{{1|94}}}}}}}| }}"]
content = content .. 't --> v[' .. table.concat(l,' ') .. ']\n'
--u --> v
content = content .. 'u --> v\n'
content = content .. 'end'
content = content .. 'end'
local graph = frame:extensionTag('mermaid', content, {})
local graph = frame:extensionTag('mermaid', content, {})

Aktuelle Version vom 3. Juni 2024, 14:17 Uhr

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