Insertion Sort
[XML]
[Smart
Pascal] [JavaScript] [Python]
[Output]
This program is an example of coding a well-known algorithm using Blockly. We use the insertion sort algorithm to sort a list of words. If necessary, see our description of the algorithm.

The blocks
XML Code of Blocks
<xml xmlns="http://www.w3.org/1999/xhtml"> <block type="variables_set" id="uSgQhvRs#]FBe^AJ8`C[" x="-612" y="-212"> <field name="VAR">strWords</field> <value name="VALUE"> <block type="text" id="ovMmv;h^/*8|TKyQJV}|"> <field name="TEXT">and,ton,ore,but,the,tor,rot,dip,off,too,two,one,mud,dim,sit,its</field> </block> </value> <next> <block type="variables_set" id="ui}o.g:f,kLpn[kods.{"> <field name="VAR">strWordsArray</field> <value name="VALUE"> <block type="lists_split" id="ndK64S%jJwS5/x$4*1C+"> <mutation mode="SPLIT"></mutation> <field name="MODE">SPLIT</field> <value name="INPUT"> <block type="variables_get" id="47UDxSZ0N!%*m8HkWz@R"> <field name="VAR">strWords</field> </block> </value> <value name="DELIM"> <shadow type="text" id=";}y9y[RUlr#)~Plt=X,a"> <field name="TEXT">,</field> </shadow> </value> </block> </value> <next> <block type="controls_for" id="9YL@_PN=qTFrKh7b|Cj}"> <field name="VAR">i</field> <value name="FROM"> <shadow type="math_number" id="ILF?CrJ9Oi5J#F@Vv6vM"> <field name="NUM">2</field> </shadow> </value> <value name="TO"> <shadow type="math_number" id="A6:Ixkw~fc6SFpRyQZ[L"> <field name="NUM">10</field> </shadow> <block type="lists_length" id="NnWP5pa#lZWNjHo[y!N2"> <value name="VALUE"> <block type="variables_get" id="~^*xz%a9@AZyj--cHHuG"> <field name="VAR">strWordsArray</field> </block> </value> </block> </value> <value name="BY"> <shadow type="math_number" id="[O[UXm6[VwI?!t?;acBq"> <field name="NUM">1</field> </shadow> </value> <statement name="DO"> <block type="variables_set" id="T/{4qq.~}=LDnRJtLWq9"> <field name="VAR">boolInserted</field> <value name="VALUE"> <block type="logic_boolean" id="Gdk:OCQ54v`0OO|-*9*R"> <field name="BOOL">FALSE</field> </block> </value> <next> <block type="variables_set" id="9FQ+KA$NKBJaKqUGfOa["> <field name="VAR">intCurrent</field> <value name="VALUE"> <block type="variables_get" id="6Fk]=V^:hsEJ|cofN8N!"> <field name="VAR">i</field> </block> </value> <next> <block type="variables_set" id="cj6MY4S2xK:OISjGhO^A"> <field name="VAR">strTemp</field> <value name="VALUE"> <block type="lists_getIndex" id="Q%Y{{Z^TnVhKyI?J/^VK"> <mutation statement="false" at="true"></mutation> <field name="MODE">GET</field> <field name="WHERE">FROM_START</field> <value name="VALUE"> <block type="variables_get" id="Z{LG1rU=i^a3NKfrL/tu"> <field name="VAR">strWordsArray</field> </block> </value> <value name="AT"> <block type="variables_get" id=")hpg=Q=C6@^iHrdNLR(~"> <field name="VAR">i</field> </block> </value> </block> </value> <next> <block type="controls_whileUntil" id="*k1_(pCN%iz3~5bn}e|]"> <field name="MODE">WHILE</field> <value name="BOOL"> <block type="logic_negate" id="z]H+Ra-QT.5uK!QJ+8^u"> <value name="BOOL"> <block type="variables_get" id="C[zPuYL9/rYe_(8te|Pm"> <field name="VAR">boolInserted</field> </block> </value> </block> </value> <statement name="DO"> <block type="controls_if" id="AcbQ1t%,1D1@(0VR@A]V"> <mutation else="1"></mutation> <value name="IF0"> <block type="logic_compare" id="uLbvQQrmFh$O44,9[#w5"> <field name="OP">GT</field> <value name="A"> <block type="variables_get" id="6|U839i`gE+HmUif1}eY"> <field name="VAR">strTemp</field> </block> </value> <value name="B"> <block type="lists_getIndex" id="$M%u(JRHR,H+)Q]N8vSM"> <mutation statement="false" at="true"></mutation> <field name="MODE">GET</field> <field name="WHERE">FROM_START</field> <value name="VALUE"> <block type="variables_get" id="Gix$}r0xbU@!l1h]DY#c"> <field name="VAR">strWordsArray</field> </block> </value> <value name="AT"> <block type="math_arithmetic" id="nmhXoHORN1wY*]`bMhrc"> <field name="OP">MINUS</field> <value name="A"> <shadow type="math_number" id="ZX1z@gs0o_K{MJeTUIC^"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="LyQrpcgwC`Fu?D}oXQWC"> <field name="VAR">intCurrent</field> </block> </value> <value name="B"> <shadow type="math_number" id="YcFes~MOH^}z1TpM*Ajl"> <field name="NUM">1</field> </shadow> </value> </block> </value> </block> </value> </block> </value> <statement name="DO0"> <block type="variables_set" id="N`6jZ{O)Kh4e=$2mMX6C"> <field name="VAR">boolInserted</field> <value name="VALUE"> <block type="logic_boolean" id="#_:;0.|1PiENQ!q1fRtJ"> <field name="BOOL">TRUE</field> </block> </value> <next> <block type="lists_setIndex" id="n,3f(bS^Db6PW;3uio[;"> <mutation at="true"></mutation> <field name="MODE">SET</field> <field name="WHERE">FROM_START</field> <value name="LIST"> <block type="variables_get" id="o$qs3NzywwfYP?ag(^v{"> <field name="VAR">strWordsArray</field> </block> </value> <value name="AT"> <block type="variables_get" id="M;v=f0/U_zAFqpr8;}ja"> <field name="VAR">intCurrent</field> </block> </value> <value name="TO"> <block type="variables_get" id="73S.w%,L=_wwA1,e*8`f"> <field name="VAR">strTemp</field> </block> </value> </block> </next> </block> </statement> <statement name="ELSE"> <block type="lists_setIndex" id=":9D,sAKoIXS55vXSB@#@"> <mutation at="true"></mutation> <field name="MODE">SET</field> <field name="WHERE">FROM_START</field> <value name="LIST"> <block type="variables_get" id="`1{^Um*Ju0[Ubl|B!-HI"> <field name="VAR">strWordsArray</field> </block> </value> <value name="AT"> <block type="variables_get" id="Onm?[9F?d4}Bi|Q:8Q$+"> <field name="VAR">intCurrent</field> </block> </value> <value name="TO"> <block type="lists_getIndex" id="9TF7R,w8CW_-?1z!_/Z("> <mutation statement="false" at="true"></mutation> <field name="MODE">GET</field> <field name="WHERE">FROM_START</field> <value name="VALUE"> <block type="variables_get" id="=llwI!NC7S$a_nnH8qhY"> <field name="VAR">strWordsArray</field> </block> </value> <value name="AT"> <block type="math_arithmetic" id="{=V79Yz:@;Oi45x|4OW."> <field name="OP">MINUS</field> <value name="A"> <shadow type="math_number" id="ZX1z@gs0o_K{MJeTUIC^"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="E5g{*2Epk{U5ArcI1kaY"> <field name="VAR">intCurrent</field> </block> </value> <value name="B"> <shadow type="math_number" id="B^N8u9^:baSTFGmR`4m~"> <field name="NUM">1</field> </shadow> </value> </block> </value> </block> </value> <next> <block type="math_change" id="b0qOf5GRrM^xMU]r{rR-"> <field name="VAR">intCurrent</field> <value name="DELTA"> <shadow type="math_number" id="=Qxmt-G33@Kz}Gm,;M*y"> <field name="NUM">-1</field> </shadow> </value> <next> <block type="controls_if" id="LM}{wuS$bVybyYAB2`os"> <value name="IF0"> <block type="logic_compare" id="01lDpQS^I:_wC3MdmxA#"> <field name="OP">EQ</field> <value name="A"> <block type="variables_get" id="AK$3j4g`(@)c7:@6TWm["> <field name="VAR">intCurrent</field> </block> </value> <value name="B"> <block type="math_number" id="xL]$0i.rU~2Q@1:/(RF/"> <field name="NUM">1</field> </block> </value> </block> </value> <statement name="DO0"> <block type="lists_setIndex" id="8[uckSH!hB[v.8+meE$O"> <mutation at="false"></mutation> <field name="MODE">SET</field> <field name="WHERE">FIRST</field> <value name="LIST"> <block type="variables_get" id="@@vau8aAXkQpu25kq$N!"> <field name="VAR">strWordsArray</field> </block> </value> <value name="TO"> <block type="variables_get" id="g`DA)l9WEexNbo}*,fY["> <field name="VAR">strTemp</field> </block> </value> <next> <block type="variables_set" id=",.NPsz1w?{sRna0nwf*Q"> <field name="VAR">boolInserted</field> <value name="VALUE"> <block type="logic_boolean" id="d-d^9/u2R2do!!.;L5hJ"> <field name="BOOL">TRUE</field> </block> </value> </block> </next> </block> </statement> </block> </next> </block> </next> </block> </statement> </block> </statement> </block> </next> </block> </next> </block> </next> </block> </statement> <next> <block type="variables_set" id="lskv7BMqCUuXX4Q5.*]L"> <field name="VAR">strWordsSorted</field> <value name="VALUE"> <block type="lists_split" id="mU*6o/kJtS7l^Dnz;0%{"> <mutation mode="JOIN"></mutation> <field name="MODE">JOIN</field> <value name="INPUT"> <block type="variables_get" id="[DM%!)!_2[pm%qzLjmgV"> <field name="VAR">strWordsArray</field> </block> </value> <value name="DELIM"> <shadow type="text" id="|,mW5IRBW!k`%WX|oIA."> <field name="TEXT">,</field> </shadow> </value> </block> </value> <next> <block type="text_print" id="_Pn#I*EZcu0`G8s)04(j"> <value name="TEXT"> <shadow type="text" id="Ag`e89f]6bs5]a-[wUjy"> <field name="TEXT">abc</field> </shadow> <block type="variables_get" id="?d-@o0oFdvNnQ8iCtSf5"> <field name="VAR">strWordsSorted</field> </block> </value> </block> </next> </block> </next> </block> </next> </block> </next> </block> </xml>
Generated Smart Pascal Code
var boolInserted: Boolean; var intCurrent: Integer; var strTemp: String; var strWords: String; var strWordsArray: TStrArray; var strWordsSorted: String; strWords := 'and,ton,ore,but,the,tor,rot,dip,off,too,two,one,mud,dim,sit,its'; strWordsArray := StrSplit(strWords, ','); for var i := 2 to strWordsArray.length step 1 do begin boolInserted := false; intCurrent := i; strTemp := strWordsArray[(i - 1)]; while ( not boolInserted) do begin if ((strTemp > strWordsArray[((intCurrent - 1) - 1)])) then begin boolInserted := true; strWordsArray[(intCurrent - 1)] := strTemp; end else begin strWordsArray[(intCurrent - 1)] := strWordsArray[((intCurrent - 1) - 1)]; intCurrent += -1; if ((intCurrent = 1)) then begin strWordsArray[0] := strTemp; boolInserted := true; end; end; end; end; strWordsSorted := StrJoin(strWordsArray, ','); Console.writeln(strWordsSorted);
Generated Javascript
var strWordsSorted, strWordsArray, boolInserted, strTemp, intCurrent, i, strWords; strWords = 'and,ton,ore,but,the,tor,rot,dip,off,too,two,one,mud,dim,sit,its'; strWordsArray = strWords.split(','); var i_end = strWordsArray.length; var i_inc = 1; if (2 > i_end) { i_inc = -i_inc; } for (i = 2; i_inc >= 0 ? i <= i_end : i >= i_end; i += i_inc) { boolInserted = false; intCurrent = i; strTemp = strWordsArray[(i - 1)]; while (!boolInserted) { if (strTemp > strWordsArray[((intCurrent - 1) - 1)]) { boolInserted = true; strWordsArray[(intCurrent - 1)] = strTemp; } else { strWordsArray[(intCurrent - 1)] = strWordsArray[((intCurrent - 1) - 1)]; intCurrent = (typeof intCurrent == 'number' ? intCurrent : 0) + -1; if (intCurrent == 1) { strWordsArray[0] = strTemp; boolInserted = true; } } } } strWordsSorted = strWordsArray.join(','); window.alert(strWordsSorted);
Generated Python
from numbers import Number strWordsSorted = None strWordsArray = None boolInserted = None strTemp = None intCurrent = None i = None strWords = None def upRange(start, stop, step): while start <= stop: yield start start += abs(step) def downRange(start, stop, step): while start >= stop: yield start start -= abs(step) strWords = 'and,ton,ore,but,the,tor,rot,dip,off,too,two,one,mud,dim,sit,its' strWordsArray = strWords.split(',') i_end = float(len(strWordsArray)) for i in (2 <= i_end) and upRange(2, i_end, 1) or downRange(2, i_end, 1): boolInserted = False intCurrent = i strTemp = strWordsArray[int(i - 1)] while not boolInserted: if strTemp > strWordsArray[int((intCurrent - 1) - 1)]: boolInserted = True strWordsArray[int(intCurrent - 1)] = strTemp else: strWordsArray[int(intCurrent - 1)] = strWordsArray[int((intCurrent - 1) - 1)] intCurrent = (intCurrent if isinstance(intCurrent, Number) else 0) + -1 if intCurrent == 1: strWordsArray[0] = strTemp boolInserted = True strWordsSorted = ','.join(strWordsArray) print(strWordsSorted)
Copy of Output
and,but,dim,dip,its,mud,off,one,ore,rot,sit,the,ton,too,tor,two