Skip to content

Instantly share code, notes, and snippets.

@neizod
Created June 14, 2012 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neizod/2931116 to your computer and use it in GitHub Desktop.
Save neizod/2931116 to your computer and use it in GitHub Desktop.
[Blockly] Quicksort - Recursive

Quicksort

Since Blockly doesn't accept function's argument & every variables is global. So I made a recursive stack just for this.

See the picture or copy this xml code and paste into XML tab in this page to see live demo yourself.

May the code be with you. :3

<xml>
<block type="procedures_defreturn" inline="false" x="-106" y="-162">
<title name="NAME">quicksort</title>
<statement name="STACK">
<block type="procedures_callnoreturn">
<mutation name="init recursive stack"></mutation>
<next>
<block type="controls_if" inline="false">
<mutation else="1"></mutation>
<value name="IF0">
<block type="logic_compare" inline="true">
<value name="A">
<block type="lists_length" inline="false">
<value name="VALUE">
<block type="variables_get">
<title name="VAR">t</title>
</block>
</value>
</block>
</value>
<value name="B" label="&gt;">
<block type="math_number">
<title name="NUM">1</title>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="variables_set" inline="false">
<title name="VAR">f</title>
<value name="VALUE">
<block type="lists_getIndex" inline="true">
<value name="AT">
<block type="math_number">
<title name="NUM">1</title>
</block>
</value>
<value name="VALUE">
<block type="variables_get">
<title name="VAR">t</title>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">t</title>
<value name="VALUE">
<block type="procedures_callreturn">
<mutation name="get text body"></mutation>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">a</title>
<value name="VALUE">
<block type="text">
<title name="TEXT"></title>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">b</title>
<value name="VALUE">
<block type="text">
<title name="TEXT"></title>
</block>
</value>
<next>
<block type="controls_forEach" inline="false">
<variable data="c" name="VAR"></variable>
<value name="LIST">
<block type="variables_get">
<title name="VAR">t</title>
</block>
</value>
<statement name="DO">
<block type="controls_if" inline="false">
<mutation else="1"></mutation>
<value name="IF0">
<block type="logic_compare" inline="true">
<value name="A">
<block type="variables_get">
<title name="VAR">c</title>
</block>
</value>
<value name="B" label="≤">
<block type="variables_get">
<title name="VAR">f</title>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="variables_set" inline="false">
<title name="VAR">a</title>
<value name="VALUE">
<block type="text_join" inline="false">
<mutation items="2"></mutation>
<value name="ADD0">
<block type="variables_get">
<title name="VAR">a</title>
</block>
</value>
<value name="ADD1">
<block type="variables_get">
<title name="VAR">c</title>
</block>
</value>
</block>
</value>
</block>
</statement>
<statement name="ELSE">
<block type="variables_set" inline="false">
<title name="VAR">b</title>
<value name="VALUE">
<block type="text_join" inline="false">
<mutation items="2"></mutation>
<value name="ADD0">
<block type="variables_get">
<title name="VAR">b</title>
</block>
</value>
<value name="ADD1">
<block type="variables_get">
<title name="VAR">c</title>
</block>
</value>
</block>
</value>
</block>
</statement>
</block>
</statement>
<next>
<block type="variables_set" inline="false">
<title name="VAR">t</title>
<value name="VALUE">
<block type="variables_get">
<title name="VAR">a</title>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">a</title>
<value name="VALUE">
<block type="procedures_callreturn">
<mutation name="quicksort"></mutation>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">t</title>
<value name="VALUE">
<block type="variables_get">
<title name="VAR">b</title>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">b</title>
<value name="VALUE">
<block type="procedures_callreturn">
<mutation name="quicksort"></mutation>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">z</title>
<value name="VALUE">
<block type="text_join" inline="false">
<mutation items="3"></mutation>
<value name="ADD0">
<block type="variables_get">
<title name="VAR">a</title>
</block>
</value>
<value name="ADD1">
<block type="variables_get">
<title name="VAR">f</title>
</block>
</value>
<value name="ADD2">
<block type="variables_get">
<title name="VAR">b</title>
</block>
</value>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
<statement name="ELSE">
<block type="variables_set" inline="false">
<title name="VAR">z</title>
<value name="VALUE">
<block type="variables_get">
<title name="VAR">t</title>
</block>
</value>
</block>
</statement>
<next>
<block type="procedures_callnoreturn">
<mutation name="destroy recursive stack"></mutation>
</block>
</next>
</block>
</next>
</block>
</statement>
<value name="RETURN">
<block type="variables_get">
<title name="VAR">z</title>
</block>
</value>
</block>
<block type="procedures_defreturn" inline="false" x="369" y="56">
<title name="NAME">get text body</title>
<statement name="STACK">
<block type="variables_set" inline="false">
<title name="VAR">z</title>
<value name="VALUE">
<block type="text">
<title name="TEXT"></title>
</block>
</value>
<next>
<block type="controls_for" inline="true">
<variable data="i" name="VAR"></variable>
<value name="FROM">
<block type="math_number">
<title name="NUM">2</title>
</block>
</value>
<value name="TO">
<block type="text_length" inline="false">
<value name="VALUE">
<block type="variables_get">
<title name="VAR">t</title>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="variables_set" inline="false">
<title name="VAR">z</title>
<value name="VALUE">
<block type="text_join" inline="false">
<mutation items="2"></mutation>
<value name="ADD0">
<block type="variables_get">
<title name="VAR">z</title>
</block>
</value>
<value name="ADD1">
<block type="text_charAt" inline="true">
<value name="AT">
<block type="variables_get">
<title name="VAR">i</title>
</block>
</value>
<value name="VALUE">
<block type="variables_get">
<title name="VAR">t</title>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</statement>
</block>
</next>
</block>
</statement>
<value name="RETURN">
<block type="variables_get">
<title name="VAR">z</title>
</block>
</value>
</block>
<block type="procedures_defnoreturn" x="365" y="-182">
<title name="NAME">init recursive stack</title>
<statement name="STACK">
<block type="math_change" inline="false">
<title name="VAR">d</title>
<value name="DELTA">
<block type="math_number">
<title name="NUM">1</title>
</block>
</value>
<next>
<block type="lists_setIndex" inline="true">
<value name="AT">
<block type="variables_get">
<title name="VAR">d</title>
</block>
</value>
<value name="LIST">
<block type="variables_get">
<title name="VAR">stack a</title>
</block>
</value>
<value name="TO">
<block type="variables_get">
<title name="VAR">a</title>
</block>
</value>
<next>
<block type="lists_setIndex" inline="true">
<value name="AT">
<block type="variables_get">
<title name="VAR">d</title>
</block>
</value>
<value name="LIST">
<block type="variables_get">
<title name="VAR">stack b</title>
</block>
</value>
<value name="TO">
<block type="variables_get">
<title name="VAR">b</title>
</block>
</value>
<next>
<block type="lists_setIndex" inline="true">
<value name="AT">
<block type="variables_get">
<title name="VAR">d</title>
</block>
</value>
<value name="LIST">
<block type="variables_get">
<title name="VAR">stack f</title>
</block>
</value>
<value name="TO">
<block type="variables_get">
<title name="VAR">f</title>
</block>
</value>
<next>
<block type="lists_setIndex" inline="true">
<value name="AT">
<block type="variables_get">
<title name="VAR">d</title>
</block>
</value>
<value name="LIST">
<block type="variables_get">
<title name="VAR">stack t</title>
</block>
</value>
<value name="TO">
<block type="variables_get">
<title name="VAR">t</title>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="881" y="-175">
<title name="NAME">destroy recursive stack</title>
<statement name="STACK">
<block type="variables_set" inline="false">
<title name="VAR">a</title>
<value name="VALUE">
<block type="lists_getIndex" inline="true">
<value name="AT">
<block type="variables_get">
<title name="VAR">d</title>
</block>
</value>
<value name="VALUE">
<block type="variables_get">
<title name="VAR">stack a</title>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">b</title>
<value name="VALUE">
<block type="lists_getIndex" inline="true">
<value name="AT">
<block type="variables_get">
<title name="VAR">d</title>
</block>
</value>
<value name="VALUE">
<block type="variables_get">
<title name="VAR">stack b</title>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">f</title>
<value name="VALUE">
<block type="lists_getIndex" inline="true">
<value name="AT">
<block type="variables_get">
<title name="VAR">d</title>
</block>
</value>
<value name="VALUE">
<block type="variables_get">
<title name="VAR">stack f</title>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">t</title>
<value name="VALUE">
<block type="lists_getIndex" inline="true">
<value name="AT">
<block type="variables_get">
<title name="VAR">d</title>
</block>
</value>
<value name="VALUE">
<block type="variables_get">
<title name="VAR">stack t</title>
</block>
</value>
</block>
</value>
<next>
<block type="math_change" inline="false">
<title name="VAR">d</title>
<value name="DELTA">
<block type="math_number">
<title name="NUM">-1</title>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="370" y="288">
<title name="NAME">init</title>
<statement name="STACK">
<block type="variables_set" inline="false">
<title name="VAR">d</title>
<value name="VALUE">
<block type="math_number">
<title name="NUM">0</title>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">a</title>
<value name="VALUE">
<block type="text">
<title name="TEXT"></title>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">b</title>
<value name="VALUE">
<block type="text">
<title name="TEXT"></title>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">f</title>
<value name="VALUE">
<block type="text">
<title name="TEXT"></title>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">stack a</title>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">stack b</title>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">stack f</title>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
<next>
<block type="variables_set" inline="false">
<title name="VAR">stack t</title>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_callnoreturn" x="1143" y="468">
<mutation name="main"></mutation>
</block>
<block type="procedures_defnoreturn" x="983" y="315">
<title name="NAME">main</title>
<statement name="STACK">
<block type="procedures_callnoreturn">
<mutation name="init"></mutation>
<next>
<block type="variables_set" inline="false">
<title name="VAR">t</title>
<value name="VALUE">
<block type="text">
<title name="TEXT">input test case here!</title>
</block>
</value>
<next>
<block type="text_print" inline="false">
<value name="TEXT">
<block type="text_join" inline="false">
<mutation items="2"></mutation>
<value name="ADD0">
<block type="text">
<title name="TEXT">result: </title>
</block>
</value>
<value name="ADD1">
<block type="procedures_callreturn">
<mutation name="quicksort"></mutation>
</block>
</value>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</xml>
@chris312319
Copy link

Why I input and it just result the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment