Error processing request

Parameters

CONTENT_LENGTH0
REQUEST_METHODGET
REQUEST_URI/revision/How+fast+can+we+sort+5+elements?V=5
QUERY_STRINGV=5
CONTENT_TYPE
DOCUMENT_URI/revision/How+fast+can+we+sort+5+elements
DOCUMENT_ROOT/var/www/nikit/nikit/nginx/../docroot
SCGI1
SERVER_PROTOCOLHTTP/1.1
HTTPSon
REMOTE_ADDR172.69.7.89
REMOTE_PORT18048
SERVER_PORT4443
SERVER_NAMEwiki.tcl-lang.org
HTTP_HOSTwiki.tcl-lang.org
HTTP_CONNECTIONKeep-Alive
HTTP_ACCEPT_ENCODINGgzip, br
HTTP_X_FORWARDED_FOR18.216.205.123
HTTP_CF_RAY87ec8f5669646082-ORD
HTTP_X_FORWARDED_PROTOhttps
HTTP_CF_VISITOR{"scheme":"https"}
HTTP_ACCEPT*/*
HTTP_USER_AGENTMozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; [email protected])
HTTP_CF_CONNECTING_IP18.216.205.123
HTTP_CDN_LOOPcloudflare
HTTP_CF_IPCOUNTRYUS

Body


Error

Unknow state transition: LINE -> END

-code

1

-level

0

-errorstack

INNER {returnImm {Unknow state transition: LINE -> END} {}} CALL {my render_wikit {How fast can we sort 5 elements} The\ \[Tcl2002\ programming\ contest:\ problem\ 1\]\ asked\ the\ entrants\ to\nwrite\ code\ to\ replicate\ the\ functionality\ of\n======\n\ \ \ \ lsort\ -index\ 0\ -ascii\ \$list\n======\nwithout\ using\ the\ \[\[lsort\]\]\ command.\ \ It\ was\ stated\ that\ it\ was\ known\ in\ advance\nthat\ the\ list\ would\ contain\ exactly\ five\ elements.\n\nIn\ preparing\ for\ the\ contest,\ one\ of\ the\ judges\ \[KBK\]\ decided\ to\ see\ \njust\ what\ was\ possible\ if\ the\ entire\ sort\ was\ placed\ in\ straight-line\ code\ \n(that\ is,\ if\ any\ loops\ were\ unrolled).\ \nOn\ finding\ that\ performance\ improved\ by\ only\ 15%\ or\ so,\nhe\ decided\ to\ get\ more\ aggressive,\ writing\ code\ to\ do\ the\ sort\ in\ seven\ comparisons.\ \ \nDoing\ so\ requires\ some\ sophistication\;\ none\ of\ the\ familiar\ sort\ algorithms\ will\ achieve\ it:\n\ \ \ *\ bubble\ sort,\ insertion\ sort,\ merge\ sort\ and\ Quicksort\ all\ will\ require\ ten\ comparisons\ in\ the\ worst\ case.\n\ \ \ *\ a\ carefully\ coded\ merge\ sort\ requires\ eight\ comparisons\ in\ the\ worst\ case.\n\ \ \ *\ heapsort\ requires\ at\ least\ nine\ comparisons\ in\ the\ worst\ case.\nIn\ fact,\ it\ may\ be\ expected\ to\ be\ difficult,\ because\ ''n''\ comparisons\ can\ distinguish\ among\ only\ ''2**n''\ possibilities\;\ seven\ comparisons\ distinguish\ only\ 128\ cases,\ and\ there\ are\ 120\ possible\ orderings\ of\ five\ elements.\ \ Nevertheless,\ it\ is\ possible,\ using\ a\ technique\ called\ ''merge\ insertion''\;\ see\ Volume\ 3\ of\ \[Knuth\]'s\ ''The\ Art\ of\ Computer\ Programming''\ for\ the\ mathematical\ details.\n----\n=======\n#\ We\ will\ begin\ by\ extracting\ the\ five\ list\ elements\ into\ variables\ ''p0''..''p4'',\ and\ the\ five\ sort\ keys\ into\ ''x0''..''x4''.\ The\ generated\ code\ will\ use\ these\ variables.\n\n#\ The\ ''sort5''\ code\ will\ be\ too\ long\ for\ us\ to\ want\ to\ write\ it\ out\ in\ longhand,\ so\ let's\ write\ some\ Tcl\ code\ to\ generate\ it,\ instead.\ \ We\ start\ with\ some\ basics.\ \ We're\ going\ to\ want\ the\ generated\ code\ to\ be\ nicely\ indented.\ \ It\ turns\ out\ that\ the\ indentation\ will\ closely\ track\ the\ level\ of\ recursion\ in\ the\ Tcl\ code\ that\ will\ write\ the\ ''sort5''\ procedure,\ so\ we\ can\ use\ the\ following\ little\ procedure\ to\ make\ the\ spaces\ at\ the\ beginning\ of\ a\ line.\n\n\ proc\ indent\ \{\}\ \{\n\ \ \ \ return\ \[format\ \{%*s\}\ \[expr\ \{\ 4\ *\ \[info\ level\]\ -\ 12\ \}\]\ \{\}\]\n\ \}\n\n#\ The\ generated\ code\ is\ going\ to\ have\ a\ nest\ of\ constucts\ that\ all\ look\ like:\n#\ if\ \{\ \[string\ compare\ \$x1\ \$x2\]\ <=\ 0\ \}\ \{\n#\ \ \ \ \ ...\ code\ ...\n#\ \}\ else\ \{\n#\ \ \ \ \ ...\ more\ code\ ...\n#\ \}\n#\ The\ <=\ operator\ will\ be\ replaced\ with\ <\ if\ x2\ preceded\ x1\ in\ the\ original\ list.\ \ This\ replacement\ will\ guarantee\ stability.\ \ Here\ are\ procedures\ to\ generate\ the\ comparisons:\n\n\ proc\ emitCompare\ \{\ v0\ v1\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ append\ code\ \[indent\]\ \{if\ \}\ \\\{\ \\\[\ \{string\ compare\ \$x\}\ \$v0\ \{\ \$x\}\ \$v1\ \\\]\n\ \ \ \ if\ \{\ \$v0\ <\ \$v1\ \}\ \{\n\ \ \ \ \ \ \ \ append\ code\ \{\ <=\ \}\n\ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ append\ code\ \{\ <\ \}\n\ \ \ \ \}\n\ \ \ \ append\ code\ \{0\ \}\ \\\}\ \{\ \}\ \\\{\ \\n\n\ \}\n\ proc\ emitElse\ \{\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ append\ code\ \[indent\]\ \\\}\ \{\ else\ \}\ \\\{\ \\n\n\ \}\n\ proc\ emitEndIf\ \{\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ append\ code\ \[indent\]\ \\\}\ \\n\n\ \}\n\n#\ We\ also\ need\ to\ generate\ a\ \[\[return\]\]\ command\ to\ sort\ the\ list\ once\ the\ correct\ order\ of\ elements\ is\ known:\n\n\ proc\ emitReturn\ \{\ a\ b\ c\ d\ e\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ append\ code\ \[indent\]\ \{\ \ \ \ return\ \}\ \\\[\ \{list\ \$p\}\ \$a\ \{\ \$p\}\ \$b\ \{\ \$p\}\ \$c\ \\\n\ \ \ \ \ \ \ \ \{\ \$p\}\ \$d\ \{\ \$p\}\ \$e\ \\\]\ \\n\n\ \}\n\n#\ With\ these\ preliminaries\ out\ of\ the\ way,\ here's\ the\ procedure\ that\ makes\ 'sort5'.\ \ It\ has\ the\ variable\ extraction\ code\;\ everything\ else\ happens\ in\ 'pair0',\ which\ follows\ it:\n\n\ proc\ makeSort5Proc\ \{\}\ \{\n\ \ \ \ set\ code\ \{\}\n\ \ \ \ append\ code\ \{proc\ sort5\ \{\ list\ \}\ \}\ \\\{\ \\n\n\ \ \ \ for\ \{\ set\ i\ 0\ \}\ \{\ \$i\ <\ 5\ \}\ \{\ incr\ i\ \}\ \{\n\ \ \ \ \ \ \ \ append\ code\ \\\n\ \ \ \ \ \ \ \ \ \ \ \ \{\ \ \ \ set\ x\}\ \$i\ \{\ \[lindex\ \[set\ p\}\ \$i\ \{\ \[lindex\ \$list\ \}\ \$i\ \{\]\]\ 0\]\}\ \\n\n\ \ \ \ \}\n\ \ \ \ pair0\ code\n\ \ \ \ append\ code\ \\\}\ \\n\n\ \ \ \ return\ \$code\n\ \}\n\n#\ On\ entry\ to\ the\ code\ generated\ by\ 'pair0',\ nothing\ is\ known\ about\ the\ order\ of\ the\ elements.\ 'pair0'\ compare\ elements\ 0\ and\ 1,\ and\ calls\ 'pair1'\ with\ the\ element\ indices\ correctly\ ordered:\n\n\ proc\ pair0\ \{\ codeVar\ \}\ \{\n\ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ emitCompare\ 0\ 1\ code\n\ \ \ \ pair1\ 0\ 1\ code\n\ \ \ \ emitElse\ code\n\ \ \ \ pair1\ 1\ 0\ code\n\ \ \ \ emitEndIf\ code\n\ \}\n\n#\ On\ entry\ to\ 'pair1',\ it\ is\ known\ that\ the\ element\ at\ index\ ''a''\ precedes\ the\ element\ at\ index\ ''b''.\ Nothing\ else\ is\ known.\ \ The\ 'pair1'\ procedure\ compares\ elements\ 2\ and\ 3,\ and\ calls\ 'pair2'\ with\ the\ results:\n\n\ proc\ pair1\ \{\ a\ b\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ emitCompare\ 2\ 3\ code\n\ \ \ \ pair2\ \$a\ \$b\ 2\ 3\ code\n\ \ \ \ emitElse\ code\n\ \ \ \ pair2\ \$a\ \$b\ 3\ 2\ code\n\ \ \ \ emitEndIf\ code\n\ \}\n\n#\ On\ entry\ to\ 'pair2',\ it\ is\ known\ that\ the\ element\ at\ index\ ''a''\ precedes\ the\ one\ at\ index\ ''b''\ and\ that\ the\ one\ at\ index\ ''c''\ precedes\ the\ one\ at\ index\ ''d''.\ Nothing\ has\ yet\ examined\ element\ 4.\ The\ 'pair2'\ procedure\ compares\ elements\ ''b''\ and\ ''d'',\ and\ calls\ 'place5'\ with\ the\ results.\n\n\ proc\ pair2\ \{\ a\ b\ c\ d\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ emitCompare\ \$b\ \$d\ code\n\ \ \ \ place5\ \$a\ \$b\ \$c\ \$d\ code\n\ \ \ \ emitElse\ code\n\ \ \ \ place5\ \$c\ \$d\ \$a\ \$b\ code\n\ \ \ \ emitEndIf\ code\n\ \}\n\n#\ After\ three\ comparisons,\ things\ begin\ to\ get\ interesting.\ Let's\ represent\ the\ relationships\ by\ connecting\ the\ elements\ with\ lines.\ If\ element\ ''a''\ is\ known\ to\ precede\ element\ ''b'',\ then\ a\ line\ joins\ ''a''\ and\ ''b''\ with\ ''a''\ at\ the\ left\ end.\ What's\ known\ so\ far\ is:\n#\ \ \ \ \ b---d\n#\ \ \ \ /\ \ \ /\n#\ \ \ a\ \ \ c\n#\ Nothing\ is\ known\ about\ element\ 4.\ Our\ next\ plan\ is\ to\ use\ binary\ search\ to\ insert\ it\ into\ the\ chain\ of\ elements\ ''a'',\ ''b''\ and\ ''d'',\ so\ we\ begin\ by\ comparing\ it\ with\ element\ ''b''.\n\n\ proc\ place5\ \{\ a\ b\ c\ d\ codeVar\ \}\ \{\ \n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ 4\ \$b\ code\n\ \ \ \ \ place5a\ \$a\ \$b\ \$c\ \$d\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ place5b\ \$a\ \$b\ \$c\ \$d\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ We\ now\ have\ done\ four\ comparisons,\ and\ established\ the\ relationships:\n\ #\ \ \ \ a---b---d\n\ #\ \ \ \ \ \ \ /\ \ \ /\n\ #\ \ \ \ \ \ 4\ \ \ c\n#\ Continue\ the\ binary\ search\ by\ comparing\ element\ 4\ against\ element\ ''a''.\n\n\ proc\ place5a\ \{\ a\ b\ c\ d\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ 4\ \$a\ code\n\ \ \ \ \ insert3\ 4\ \$a\ \$b\ \$d\ \$c\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ insert3\ \$a\ 4\ \$b\ \$d\ \$c\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ In\ this\ procedure,\ we\ have\ done\ four\ comparisons,\ and\ established\ the\ relationships:\n#\ \ \ \ \ \ \ \ \ \ 4\n#\ \ \ \ \ \ \ \ \ /\n#\ \ \ \ a---b---d\ \n#\ \ \ \ \ \ \ \ \ \ \ /\n#\ \ \ \ \ \ \ \ \ \ c\n#\ Continue\ the\ binary\ search\ by\ comparing\ element\ 4\ agains\ element\ ''d''.\n\n\ proc\ place5b\ \{\ a\ b\ c\ d\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ 4\ \$d\ code\n\ \ \ \ \ insert3\ \$a\ \$b\ 4\ \$d\ \$c\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ insert2\ \$a\ \$b\ \$d\ 4\ \$c\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ Five\ comparisons\ have\ completed\ the\ binary\ search\ that\ inserts\ the\ last\ element\ into\ a\ three-element\ chain.\ We\ now\ have\ the\ relationships:\n#\ \ \ \ a---b---c---d\n#\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ /\n#\ \ \ \ \ \ \ \ \ \ \ \ \ \ e\n#\ We\ start\ another\ binary\ search\ to\ insert\ element\ ''e''\ into\ the\ chain\n#\ formed\ by\ elements\ ''a'',\ ''b''\ and\ ''c''.\ Once\ again,\ things\ begin\ by\n#\ comparing\ it\ against\ the\ middle\ element\ ''b'':\n\n\ proc\ insert3\ \{\ a\ b\ c\ d\ e\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ \$e\ \$b\ code\n\ \ \ \ \ insert3a\ \$a\ \$b\ \$c\ \$d\ \$e\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ insert3b\ \$a\ \$b\ \$c\ \$d\ \$e\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ Six\ comparisons\ have\ established:\n#\ \ \ \ a---b---c---d\n#\ \ \ \ \ \ \ /\n#\ \ \ \ \ \ e\n#\ One\ more\ comparison\ determines\ the\ sequence.\n\n\ proc\ insert3a\ \{\ a\ b\ c\ d\ e\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ \$e\ \$a\ code\n\ \ \ \ \ emitReturn\ \$e\ \$a\ \$b\ \$c\ \$d\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ emitReturn\ \$a\ \$e\ \$b\ \$c\ \$d\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ Six\ comparisons\ have\ established:\n#\ \ \ \ \ \ \ \ \ c\n#\ \ \ \ \ \ \ \ /\ \\\ \n#\ \ \ a---b\ \ \ d\n#\ \ \ \ \ \ \ \ \\\ /\n#\ \ \ \ \ \ \ \ \ e\n#\ One\ more\ comparison\ determines\ the\ order\ of\ ''c''\ and\ ''e''.\n\n\ proc\ insert3b\ \{\ a\ b\ c\ d\ e\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ \$e\ \$c\ code\n\ \ \ \ \ emitReturn\ \$a\ \$b\ \$e\ \$c\ \$d\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ emitReturn\ \$a\ \$b\ \$c\ \$e\ \$d\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ The\ next\ procedure\ handles\ the\ (lucky)\ case\ where\ five\ comparisons\ establish\ the\ relationships:\n#\ \ \ \ a---b---c---d\n#\ \ \ \ \ \ \ \ \ \ \ /\n#\ \ \ \ \ \ \ \ \ \ e\n#\ Compare\ ''e''\ against\ ''b'',\ then\ against\ ''a''\ if\ necessary.\n\n\ proc\ insert2\ \{\ a\ b\ c\ d\ e\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ \$e\ \$b\ code\n\ \ \ \ \ insert3a\ \$a\ \$b\ \$c\ \$d\ \$e\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ emitReturn\ \$a\ \$b\ \$e\ \$c\ \$d\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ A\ call\ to\ \[\[makeSort5Proc\]\]\ finishes\ the\ job:\n\ eval\ \[makeSort5Proc\]\n======\n----\nThe\ generated\ procedure\ is\ 485\ lines\ long,\ and\ consists\ entirely\ of\ straight-line\ comparisons.\ \ It\ begins:\n======\n\ proc\ sort5\ \{\ list\ \}\ \{\n\ \ \ \ set\ x0\ \[lindex\ \[set\ p0\ \[lindex\ \$list\ 0\]\]\ 0\]\n\ \ \ \ set\ x1\ \[lindex\ \[set\ p1\ \[lindex\ \$list\ 1\]\]\ 0\]\n\ \ \ \ set\ x2\ \[lindex\ \[set\ p2\ \[lindex\ \$list\ 2\]\]\ 0\]\n\ \ \ \ set\ x3\ \[lindex\ \[set\ p3\ \[lindex\ \$list\ 3\]\]\ 0\]\n\ \ \ \ set\ x4\ \[lindex\ \[set\ p4\ \[lindex\ \$list\ 4\]\]\ 0\]\n\ \ \ \ if\ \{\[string\ compare\ \$x0\ \$x1\]\ <=\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x3\]\ <=\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x1\ \$x3\]\ <=\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x4\ \$x1\]\ <\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x4\ \$x0\]\ <\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x0\]\ <\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x4\]\ <=\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p2\ \$p4\ \$p0\ \$p1\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p4\ \$p2\ \$p0\ \$p1\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x1\]\ <\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p4\ \$p0\ \$p2\ \$p1\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p4\ \$p0\ \$p1\ \$p2\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x4\]\ <=\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x0\]\ <\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p2\ \$p0\ \$p4\ \$p1\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p0\ \$p2\ \$p4\ \$p1\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ .\ .\ .\n======\n----\nThe\ generated\ procedure\ needs\ only\ 141%\ of\ the\ time\ that\ a\ procedure\ consisting\ of\ the\ \[lsort\]\ command\ uses.\ \ There\ is\ at\ least\ one\ way\ to\ squeeze\ a\ tiny\ bit\ more\ out\ of\ it,\ by\ removing\ the\ 'set'\ statements\ at\ the\ top\ of\ the\ procedure,\ and\ instead\ placing\ them\ in-line\ in\ the\ first\ place\ that\ the\ corresponding\ variables\ appear\ on\ any\ branch\ through\ the\ code.\ \ \[KBK\]\ has\ tried\ this,\ and\ discovered\ that\ it\ saves\ less\ than\ 2%\ of\ the\ remaining\ time.\ \ We\ appear\ here\ to\ be\ close\ to\ the\ absolute\ minimum\ run\ time!\n----\nIf\ you\ want\ to\ try\ things\ out\ for\ yourself,\ the\ program\ that\ the\ judges\ used\ \nto\ evaluate\ contestants\ is\ at\ \[Tcl2002\ programming\ contest:\ problem\ 1\ test\ harness\]\n----\n**See\ Also**\n\ \ \ *\ \[Tcl2002\ programming\ contest:\ problem\ 1\]\n\ \ \ *\ \[Tcl\ 2002\ programming\ contest:\ solutions\ to\ problem\ 1\]\n\ \ \ *\ \[The\ Great\ Canadian\ Tcl/Tk\ Programming\ Contest\],\ eh?\n\n<<categories>>\ Performance regexp2} CALL {my render {How fast can we sort 5 elements} The\ \[Tcl2002\ programming\ contest:\ problem\ 1\]\ asked\ the\ entrants\ to\nwrite\ code\ to\ replicate\ the\ functionality\ of\n======\n\ \ \ \ lsort\ -index\ 0\ -ascii\ \$list\n======\nwithout\ using\ the\ \[\[lsort\]\]\ command.\ \ It\ was\ stated\ that\ it\ was\ known\ in\ advance\nthat\ the\ list\ would\ contain\ exactly\ five\ elements.\n\nIn\ preparing\ for\ the\ contest,\ one\ of\ the\ judges\ \[KBK\]\ decided\ to\ see\ \njust\ what\ was\ possible\ if\ the\ entire\ sort\ was\ placed\ in\ straight-line\ code\ \n(that\ is,\ if\ any\ loops\ were\ unrolled).\ \nOn\ finding\ that\ performance\ improved\ by\ only\ 15%\ or\ so,\nhe\ decided\ to\ get\ more\ aggressive,\ writing\ code\ to\ do\ the\ sort\ in\ seven\ comparisons.\ \ \nDoing\ so\ requires\ some\ sophistication\;\ none\ of\ the\ familiar\ sort\ algorithms\ will\ achieve\ it:\n\ \ \ *\ bubble\ sort,\ insertion\ sort,\ merge\ sort\ and\ Quicksort\ all\ will\ require\ ten\ comparisons\ in\ the\ worst\ case.\n\ \ \ *\ a\ carefully\ coded\ merge\ sort\ requires\ eight\ comparisons\ in\ the\ worst\ case.\n\ \ \ *\ heapsort\ requires\ at\ least\ nine\ comparisons\ in\ the\ worst\ case.\nIn\ fact,\ it\ may\ be\ expected\ to\ be\ difficult,\ because\ ''n''\ comparisons\ can\ distinguish\ among\ only\ ''2**n''\ possibilities\;\ seven\ comparisons\ distinguish\ only\ 128\ cases,\ and\ there\ are\ 120\ possible\ orderings\ of\ five\ elements.\ \ Nevertheless,\ it\ is\ possible,\ using\ a\ technique\ called\ ''merge\ insertion''\;\ see\ Volume\ 3\ of\ \[Knuth\]'s\ ''The\ Art\ of\ Computer\ Programming''\ for\ the\ mathematical\ details.\n----\n=======\n#\ We\ will\ begin\ by\ extracting\ the\ five\ list\ elements\ into\ variables\ ''p0''..''p4'',\ and\ the\ five\ sort\ keys\ into\ ''x0''..''x4''.\ The\ generated\ code\ will\ use\ these\ variables.\n\n#\ The\ ''sort5''\ code\ will\ be\ too\ long\ for\ us\ to\ want\ to\ write\ it\ out\ in\ longhand,\ so\ let's\ write\ some\ Tcl\ code\ to\ generate\ it,\ instead.\ \ We\ start\ with\ some\ basics.\ \ We're\ going\ to\ want\ the\ generated\ code\ to\ be\ nicely\ indented.\ \ It\ turns\ out\ that\ the\ indentation\ will\ closely\ track\ the\ level\ of\ recursion\ in\ the\ Tcl\ code\ that\ will\ write\ the\ ''sort5''\ procedure,\ so\ we\ can\ use\ the\ following\ little\ procedure\ to\ make\ the\ spaces\ at\ the\ beginning\ of\ a\ line.\n\n\ proc\ indent\ \{\}\ \{\n\ \ \ \ return\ \[format\ \{%*s\}\ \[expr\ \{\ 4\ *\ \[info\ level\]\ -\ 12\ \}\]\ \{\}\]\n\ \}\n\n#\ The\ generated\ code\ is\ going\ to\ have\ a\ nest\ of\ constucts\ that\ all\ look\ like:\n#\ if\ \{\ \[string\ compare\ \$x1\ \$x2\]\ <=\ 0\ \}\ \{\n#\ \ \ \ \ ...\ code\ ...\n#\ \}\ else\ \{\n#\ \ \ \ \ ...\ more\ code\ ...\n#\ \}\n#\ The\ <=\ operator\ will\ be\ replaced\ with\ <\ if\ x2\ preceded\ x1\ in\ the\ original\ list.\ \ This\ replacement\ will\ guarantee\ stability.\ \ Here\ are\ procedures\ to\ generate\ the\ comparisons:\n\n\ proc\ emitCompare\ \{\ v0\ v1\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ append\ code\ \[indent\]\ \{if\ \}\ \\\{\ \\\[\ \{string\ compare\ \$x\}\ \$v0\ \{\ \$x\}\ \$v1\ \\\]\n\ \ \ \ if\ \{\ \$v0\ <\ \$v1\ \}\ \{\n\ \ \ \ \ \ \ \ append\ code\ \{\ <=\ \}\n\ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ append\ code\ \{\ <\ \}\n\ \ \ \ \}\n\ \ \ \ append\ code\ \{0\ \}\ \\\}\ \{\ \}\ \\\{\ \\n\n\ \}\n\ proc\ emitElse\ \{\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ append\ code\ \[indent\]\ \\\}\ \{\ else\ \}\ \\\{\ \\n\n\ \}\n\ proc\ emitEndIf\ \{\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ append\ code\ \[indent\]\ \\\}\ \\n\n\ \}\n\n#\ We\ also\ need\ to\ generate\ a\ \[\[return\]\]\ command\ to\ sort\ the\ list\ once\ the\ correct\ order\ of\ elements\ is\ known:\n\n\ proc\ emitReturn\ \{\ a\ b\ c\ d\ e\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ append\ code\ \[indent\]\ \{\ \ \ \ return\ \}\ \\\[\ \{list\ \$p\}\ \$a\ \{\ \$p\}\ \$b\ \{\ \$p\}\ \$c\ \\\n\ \ \ \ \ \ \ \ \{\ \$p\}\ \$d\ \{\ \$p\}\ \$e\ \\\]\ \\n\n\ \}\n\n#\ With\ these\ preliminaries\ out\ of\ the\ way,\ here's\ the\ procedure\ that\ makes\ 'sort5'.\ \ It\ has\ the\ variable\ extraction\ code\;\ everything\ else\ happens\ in\ 'pair0',\ which\ follows\ it:\n\n\ proc\ makeSort5Proc\ \{\}\ \{\n\ \ \ \ set\ code\ \{\}\n\ \ \ \ append\ code\ \{proc\ sort5\ \{\ list\ \}\ \}\ \\\{\ \\n\n\ \ \ \ for\ \{\ set\ i\ 0\ \}\ \{\ \$i\ <\ 5\ \}\ \{\ incr\ i\ \}\ \{\n\ \ \ \ \ \ \ \ append\ code\ \\\n\ \ \ \ \ \ \ \ \ \ \ \ \{\ \ \ \ set\ x\}\ \$i\ \{\ \[lindex\ \[set\ p\}\ \$i\ \{\ \[lindex\ \$list\ \}\ \$i\ \{\]\]\ 0\]\}\ \\n\n\ \ \ \ \}\n\ \ \ \ pair0\ code\n\ \ \ \ append\ code\ \\\}\ \\n\n\ \ \ \ return\ \$code\n\ \}\n\n#\ On\ entry\ to\ the\ code\ generated\ by\ 'pair0',\ nothing\ is\ known\ about\ the\ order\ of\ the\ elements.\ 'pair0'\ compare\ elements\ 0\ and\ 1,\ and\ calls\ 'pair1'\ with\ the\ element\ indices\ correctly\ ordered:\n\n\ proc\ pair0\ \{\ codeVar\ \}\ \{\n\ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ emitCompare\ 0\ 1\ code\n\ \ \ \ pair1\ 0\ 1\ code\n\ \ \ \ emitElse\ code\n\ \ \ \ pair1\ 1\ 0\ code\n\ \ \ \ emitEndIf\ code\n\ \}\n\n#\ On\ entry\ to\ 'pair1',\ it\ is\ known\ that\ the\ element\ at\ index\ ''a''\ precedes\ the\ element\ at\ index\ ''b''.\ Nothing\ else\ is\ known.\ \ The\ 'pair1'\ procedure\ compares\ elements\ 2\ and\ 3,\ and\ calls\ 'pair2'\ with\ the\ results:\n\n\ proc\ pair1\ \{\ a\ b\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ emitCompare\ 2\ 3\ code\n\ \ \ \ pair2\ \$a\ \$b\ 2\ 3\ code\n\ \ \ \ emitElse\ code\n\ \ \ \ pair2\ \$a\ \$b\ 3\ 2\ code\n\ \ \ \ emitEndIf\ code\n\ \}\n\n#\ On\ entry\ to\ 'pair2',\ it\ is\ known\ that\ the\ element\ at\ index\ ''a''\ precedes\ the\ one\ at\ index\ ''b''\ and\ that\ the\ one\ at\ index\ ''c''\ precedes\ the\ one\ at\ index\ ''d''.\ Nothing\ has\ yet\ examined\ element\ 4.\ The\ 'pair2'\ procedure\ compares\ elements\ ''b''\ and\ ''d'',\ and\ calls\ 'place5'\ with\ the\ results.\n\n\ proc\ pair2\ \{\ a\ b\ c\ d\ codeVar\ \}\ \{\n\ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ emitCompare\ \$b\ \$d\ code\n\ \ \ \ place5\ \$a\ \$b\ \$c\ \$d\ code\n\ \ \ \ emitElse\ code\n\ \ \ \ place5\ \$c\ \$d\ \$a\ \$b\ code\n\ \ \ \ emitEndIf\ code\n\ \}\n\n#\ After\ three\ comparisons,\ things\ begin\ to\ get\ interesting.\ Let's\ represent\ the\ relationships\ by\ connecting\ the\ elements\ with\ lines.\ If\ element\ ''a''\ is\ known\ to\ precede\ element\ ''b'',\ then\ a\ line\ joins\ ''a''\ and\ ''b''\ with\ ''a''\ at\ the\ left\ end.\ What's\ known\ so\ far\ is:\n#\ \ \ \ \ b---d\n#\ \ \ \ /\ \ \ /\n#\ \ \ a\ \ \ c\n#\ Nothing\ is\ known\ about\ element\ 4.\ Our\ next\ plan\ is\ to\ use\ binary\ search\ to\ insert\ it\ into\ the\ chain\ of\ elements\ ''a'',\ ''b''\ and\ ''d'',\ so\ we\ begin\ by\ comparing\ it\ with\ element\ ''b''.\n\n\ proc\ place5\ \{\ a\ b\ c\ d\ codeVar\ \}\ \{\ \n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ 4\ \$b\ code\n\ \ \ \ \ place5a\ \$a\ \$b\ \$c\ \$d\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ place5b\ \$a\ \$b\ \$c\ \$d\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ We\ now\ have\ done\ four\ comparisons,\ and\ established\ the\ relationships:\n\ #\ \ \ \ a---b---d\n\ #\ \ \ \ \ \ \ /\ \ \ /\n\ #\ \ \ \ \ \ 4\ \ \ c\n#\ Continue\ the\ binary\ search\ by\ comparing\ element\ 4\ against\ element\ ''a''.\n\n\ proc\ place5a\ \{\ a\ b\ c\ d\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ 4\ \$a\ code\n\ \ \ \ \ insert3\ 4\ \$a\ \$b\ \$d\ \$c\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ insert3\ \$a\ 4\ \$b\ \$d\ \$c\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ In\ this\ procedure,\ we\ have\ done\ four\ comparisons,\ and\ established\ the\ relationships:\n#\ \ \ \ \ \ \ \ \ \ 4\n#\ \ \ \ \ \ \ \ \ /\n#\ \ \ \ a---b---d\ \n#\ \ \ \ \ \ \ \ \ \ \ /\n#\ \ \ \ \ \ \ \ \ \ c\n#\ Continue\ the\ binary\ search\ by\ comparing\ element\ 4\ agains\ element\ ''d''.\n\n\ proc\ place5b\ \{\ a\ b\ c\ d\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ 4\ \$d\ code\n\ \ \ \ \ insert3\ \$a\ \$b\ 4\ \$d\ \$c\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ insert2\ \$a\ \$b\ \$d\ 4\ \$c\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ Five\ comparisons\ have\ completed\ the\ binary\ search\ that\ inserts\ the\ last\ element\ into\ a\ three-element\ chain.\ We\ now\ have\ the\ relationships:\n#\ \ \ \ a---b---c---d\n#\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ /\n#\ \ \ \ \ \ \ \ \ \ \ \ \ \ e\n#\ We\ start\ another\ binary\ search\ to\ insert\ element\ ''e''\ into\ the\ chain\n#\ formed\ by\ elements\ ''a'',\ ''b''\ and\ ''c''.\ Once\ again,\ things\ begin\ by\n#\ comparing\ it\ against\ the\ middle\ element\ ''b'':\n\n\ proc\ insert3\ \{\ a\ b\ c\ d\ e\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ \$e\ \$b\ code\n\ \ \ \ \ insert3a\ \$a\ \$b\ \$c\ \$d\ \$e\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ insert3b\ \$a\ \$b\ \$c\ \$d\ \$e\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ Six\ comparisons\ have\ established:\n#\ \ \ \ a---b---c---d\n#\ \ \ \ \ \ \ /\n#\ \ \ \ \ \ e\n#\ One\ more\ comparison\ determines\ the\ sequence.\n\n\ proc\ insert3a\ \{\ a\ b\ c\ d\ e\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ \$e\ \$a\ code\n\ \ \ \ \ emitReturn\ \$e\ \$a\ \$b\ \$c\ \$d\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ emitReturn\ \$a\ \$e\ \$b\ \$c\ \$d\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ Six\ comparisons\ have\ established:\n#\ \ \ \ \ \ \ \ \ c\n#\ \ \ \ \ \ \ \ /\ \\\ \n#\ \ \ a---b\ \ \ d\n#\ \ \ \ \ \ \ \ \\\ /\n#\ \ \ \ \ \ \ \ \ e\n#\ One\ more\ comparison\ determines\ the\ order\ of\ ''c''\ and\ ''e''.\n\n\ proc\ insert3b\ \{\ a\ b\ c\ d\ e\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ \$e\ \$c\ code\n\ \ \ \ \ emitReturn\ \$a\ \$b\ \$e\ \$c\ \$d\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ emitReturn\ \$a\ \$b\ \$c\ \$e\ \$d\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ The\ next\ procedure\ handles\ the\ (lucky)\ case\ where\ five\ comparisons\ establish\ the\ relationships:\n#\ \ \ \ a---b---c---d\n#\ \ \ \ \ \ \ \ \ \ \ /\n#\ \ \ \ \ \ \ \ \ \ e\n#\ Compare\ ''e''\ against\ ''b'',\ then\ against\ ''a''\ if\ necessary.\n\n\ proc\ insert2\ \{\ a\ b\ c\ d\ e\ codeVar\ \}\ \{\n\ \ \ \ \ upvar\ 1\ \$codeVar\ code\n\ \ \ \ \ emitCompare\ \$e\ \$b\ code\n\ \ \ \ \ insert3a\ \$a\ \$b\ \$c\ \$d\ \$e\ code\n\ \ \ \ \ emitElse\ code\n\ \ \ \ \ emitReturn\ \$a\ \$b\ \$e\ \$c\ \$d\ code\n\ \ \ \ \ emitEndIf\ code\n\ \}\n\n#\ A\ call\ to\ \[\[makeSort5Proc\]\]\ finishes\ the\ job:\n\ eval\ \[makeSort5Proc\]\n======\n----\nThe\ generated\ procedure\ is\ 485\ lines\ long,\ and\ consists\ entirely\ of\ straight-line\ comparisons.\ \ It\ begins:\n======\n\ proc\ sort5\ \{\ list\ \}\ \{\n\ \ \ \ set\ x0\ \[lindex\ \[set\ p0\ \[lindex\ \$list\ 0\]\]\ 0\]\n\ \ \ \ set\ x1\ \[lindex\ \[set\ p1\ \[lindex\ \$list\ 1\]\]\ 0\]\n\ \ \ \ set\ x2\ \[lindex\ \[set\ p2\ \[lindex\ \$list\ 2\]\]\ 0\]\n\ \ \ \ set\ x3\ \[lindex\ \[set\ p3\ \[lindex\ \$list\ 3\]\]\ 0\]\n\ \ \ \ set\ x4\ \[lindex\ \[set\ p4\ \[lindex\ \$list\ 4\]\]\ 0\]\n\ \ \ \ if\ \{\[string\ compare\ \$x0\ \$x1\]\ <=\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x3\]\ <=\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x1\ \$x3\]\ <=\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x4\ \$x1\]\ <\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x4\ \$x0\]\ <\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x0\]\ <\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x4\]\ <=\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p2\ \$p4\ \$p0\ \$p1\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p4\ \$p2\ \$p0\ \$p1\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x1\]\ <\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p4\ \$p0\ \$p2\ \$p1\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p4\ \$p0\ \$p1\ \$p2\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x4\]\ <=\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ \{\[string\ compare\ \$x2\ \$x0\]\ <\ 0\ \}\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p2\ \$p0\ \$p4\ \$p1\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\ else\ \{\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ \[list\ \$p0\ \$p2\ \$p4\ \$p1\ \$p3\]\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}\n\ .\ .\ .\n======\n----\nThe\ generated\ procedure\ needs\ only\ 141%\ of\ the\ time\ that\ a\ procedure\ consisting\ of\ the\ \[lsort\]\ command\ uses.\ \ There\ is\ at\ least\ one\ way\ to\ squeeze\ a\ tiny\ bit\ more\ out\ of\ it,\ by\ removing\ the\ 'set'\ statements\ at\ the\ top\ of\ the\ procedure,\ and\ instead\ placing\ them\ in-line\ in\ the\ first\ place\ that\ the\ corresponding\ variables\ appear\ on\ any\ branch\ through\ the\ code.\ \ \[KBK\]\ has\ tried\ this,\ and\ discovered\ that\ it\ saves\ less\ than\ 2%\ of\ the\ remaining\ time.\ \ We\ appear\ here\ to\ be\ close\ to\ the\ absolute\ minimum\ run\ time!\n----\nIf\ you\ want\ to\ try\ things\ out\ for\ yourself,\ the\ program\ that\ the\ judges\ used\ \nto\ evaluate\ contestants\ is\ at\ \[Tcl2002\ programming\ contest:\ problem\ 1\ test\ harness\]\n----\n**See\ Also**\n\ \ \ *\ \[Tcl2002\ programming\ contest:\ problem\ 1\]\n\ \ \ *\ \[Tcl\ 2002\ programming\ contest:\ solutions\ to\ problem\ 1\]\n\ \ \ *\ \[The\ Great\ Canadian\ Tcl/Tk\ Programming\ Contest\],\ eh?\n\n<<categories>>\ Performance} CALL {my revision {How fast can we sort 5 elements}} CALL {::oo::Obj5874648 process revision/How+fast+can+we+sort+5+elements} CALL {::oo::Obj5874646 process}

-errorcode

NONE

-errorinfo

Unknow state transition: LINE -> END
    while executing
"error $msg"
    (class "::Wiki" method "render_wikit" line 6)
    invoked from within
"my render_$default_markup $N $C $mkup_rendering_engine"
    (class "::Wiki" method "render" line 8)
    invoked from within
"my render $name $C"
    (class "::Wiki" method "revision" line 31)
    invoked from within
"my revision $page"
    (class "::Wiki" method "process" line 56)
    invoked from within
"$server process [string trim $uri /]"

-errorline

4