{"id":1666,"date":"2014-12-11T08:00:29","date_gmt":"2014-12-11T08:00:29","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=1666"},"modified":"2014-12-11T09:01:09","modified_gmt":"2014-12-11T09:01:09","slug":"quicksort-in-apl","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2014\/12\/quicksort-in-apl\/","title":{"rendered":"Quicksort in APL"},"content":{"rendered":"<p>Quicksort is a classic sorting algorithm invented by C.A.R. Hoare in 1961 [<acronym title=\"Hoare, C.A.R, Algorithm 63: Partition, Communications of the ACM, Volume 4, Number 7, 1961-07.\">0<\/acronym>, <acronym title=\"Hoare, C.A.R, Algorithm 64: Quicksort, Communications of the ACM, Volume 4, Number 7, 1961-07.\">1<\/acronym>]. It has been known for some time that quicksort has a terse rendition in APL <acronym title=\"Hui, Roger K.W., and Kenneth E. Iverson, J Introduction and Dictionary, 1991-2014; if. entry.\">[2]<\/acronym>. To get right to it, here is the code:<\/p>\n<pre><code class=\"language-apl\">Q\u2190{1\u2265\u2262\u2375:\u2375 \u22c4 S\u2190{\u237a\u233f\u2368\u237a \u237a\u237a \u2375} \u22c4 \u2375((\u2207&lt;S)\u236a=S\u236a(\u2207&gt;S))\u2375\u2337\u2368?\u2262\u2375}<\/code><\/pre>\n<p>The &#8220;pivot&#8221; <code class=\"language-apl\">\u2375\u2337\u2368?\u2262\u2375<\/code> is randomly chosen. <code class=\"language-apl\">((\u2207&lt;S)\u236a=S\u236a(\u2207&gt;S))<\/code> is a fork, selecting the parts of <code class=\"language-apl\">\u2375<\/code> which are less than the pivot, equal to the pivot, and greater than the pivot. The function is recursively applied to the first and the last of these three parts.<\/p>\n<pre><code class=\"language-apl\">      \u2395io\u21900 \u22c4 \u2395rl\u21907*5\r\n\r\n      \u2395\u2190x\u2190?13\u237420\r\n3 2 19 16 11 4 18 17 9 17 7 3 1\r\n      Q x\r\n1 2 3 3 4 7 9 11 16 17 17 18 19\r\n<\/code><\/pre>\n<p>The variant <code style=\"color:black;\">Q1<\/code> obtains by enclosing each of the three parts. Its result exhibits an interesting structure. The middle item of each triplet is the value of the pivot at each recursion. Since the pivot is randomly chosen, the result of <code style=\"color:black;\">Q1<\/code> can be different on the same argument, as illustrated below:<\/p>\n<pre>      <code style=\"color:black;\">Q1<\/code><code class=\"language-apl\">\u2190{1\u2265\u2262\u2375:\u2375 \u22c4 S\u2190{\u237a\u233f\u2368\u237a \u237a\u237a \u2375} \u22c4 \u2375((\u2282\u2218\u2207&lt;S)\u236a(\u2282=S)\u236a(\u2282\u2218\u2207&gt;S))\u2375\u2337\u2368?\u2262\u2375}\r\n\r\n      <\/code><code style=\"color:black;\">Q1<\/code><code class=\"language-apl\"> x\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502\u250c\u2500\u252c\u2500\u252c\u2510\u25023 3\u2502\u250c\u2500\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u2502\r\n\u2502\u25021\u25022\u2502\u2502\u2502   \u2502\u25024\u25027\u2502\u250c\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u2502\u2502\r\n\u2502\u2514\u2500\u2534\u2500\u2534\u2518\u2502   \u2502\u2502 \u2502 \u2502\u2502\u25029\u2502\u250c\u2500\u2500\u252c\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u2502\u2502\u2502\r\n\u2502      \u2502   \u2502\u2502 \u2502 \u2502\u2502\u2502 \u2502\u250211\u250216\u2502\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u252c\u2500\u2500\u2510\u2502\u2502\u2502\u2502\r\n\u2502      \u2502   \u2502\u2502 \u2502 \u2502\u2502\u2502 \u2502\u2502  \u2502  \u2502\u2502\u250c\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2510\u250218\u250219\u2502\u2502\u2502\u2502\u2502\r\n\u2502      \u2502   \u2502\u2502 \u2502 \u2502\u2502\u2502 \u2502\u2502  \u2502  \u2502\u2502\u2502\u250217 17\u2502\u2502\u2502  \u2502  \u2502\u2502\u2502\u2502\u2502\r\n\u2502      \u2502   \u2502\u2502 \u2502 \u2502\u2502\u2502 \u2502\u2502  \u2502  \u2502\u2502\u2514\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2518\u2502  \u2502  \u2502\u2502\u2502\u2502\u2502\r\n\u2502      \u2502   \u2502\u2502 \u2502 \u2502\u2502\u2502 \u2502\u2502  \u2502  \u2502\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2518\u2502\u2502\u2502\u2502\r\n\u2502      \u2502   \u2502\u2502 \u2502 \u2502\u2502\u2502 \u2502\u2514\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2502\u2502\u2502\r\n\u2502      \u2502   \u2502\u2502 \u2502 \u2502\u2514\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2502\u2502\r\n\u2502      \u2502   \u2502\u2514\u2500\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n\r\n      <\/code><code style=\"color:black;\">Q1<\/code><code class=\"language-apl\"> x\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u252c\u2510\u25029\u2502\u250c\u2500\u2500\u252c\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u2502\r\n\u2502\u2502\u250c\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u25027\u2502\u2502\u2502 \u2502\u250211\u250216\u2502\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u252c\u2510\u2502\u2502\r\n\u2502\u2502\u2502\u25021\u2502\u250c\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\u2502\u2502 \u2502\u2502\u2502 \u2502\u2502  \u2502  \u2502\u2502\u250c\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2510\u250219\u2502\u2502\u2502\u2502\r\n\u2502\u2502\u2502\u2502 \u2502\u2502\u25022\u2502\u250c\u252c\u2500\u2500\u2500\u252c\u2500\u2510\u2502\u2502\u2502 \u2502\u2502\u2502 \u2502\u2502  \u2502  \u2502\u2502\u2502\u250217 17\u250218\u2502\u2502  \u2502\u2502\u2502\u2502\r\n\u2502\u2502\u2502\u2502 \u2502\u2502\u2502 \u2502\u2502\u25023 3\u25024\u2502\u2502\u2502\u2502 \u2502\u2502\u2502 \u2502\u2502  \u2502  \u2502\u2502\u2514\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2518\u2502  \u2502\u2502\u2502\u2502\r\n\u2502\u2502\u2502\u2502 \u2502\u2502\u2502 \u2502\u2514\u2534\u2500\u2500\u2500\u2534\u2500\u2518\u2502\u2502\u2502 \u2502\u2502\u2502 \u2502\u2502  \u2502  \u2502\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2534\u2518\u2502\u2502\r\n\u2502\u2502\u2502\u2502 \u2502\u2514\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2502\u2502 \u2502\u2502\u2502 \u2502\u2514\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2502\r\n\u2502\u2502\u2514\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\u2502 \u2502\u2502\u2502 \u2502                         \u2502\r\n\u2502\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2534\u2518\u2502 \u2502                         \u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>The <em>enlist<\/em> of the result of <code style=\"color:black;\">Q1 x<\/code> is the same as <code class=\"language-apl\">Q\u00a0x<\/code>, the sort of <code class=\"language-apl\">x<\/code>:<\/p>\n<pre><code class=\"language-apl\">      \u220a <\/code><code style=\"color:black;\">Q1<\/code><code class=\"language-apl\"> x\r\n1 2 3 3 4 7 9 11 16 17 17 18 19\r\n      Q x\r\n1 2 3 3 4 7 9 11 16 17 17 18 19\r\n<\/code><\/pre>\n<p>This note is meant to explore the workings of a classical algorithm. To actually sort data in Dyalog, it is more convenient and more efficient to use <code class=\"language-apl\">{\u2375[\u234b\u2375]}<\/code>. Earlier versions of this text appeared in [<acronym title=\"Hui, Roger K.W., Quicksort, J Wiki Essay, 2005-09-28.\">3<\/acronym>, <acronym title=\"Hui, Roger K.W. Sixteen APL Amuse-Bouches, 2014-11-02.\">4<\/acronym>].<\/p>\n<h2>References<\/h2>\n<ol class=\"reference\">\n<li>Hoare, C.A.R, <a href=\"http:\/\/dl.acm.org\/citation.cfm?doid=366622.366642\"><em>Algorithm 63: Partition<\/em><\/a>, Communications of the ACM, Volume 4, Number 7, 1961-07.<\/li>\n<li>Hoare, C.A.R, <a href=\"http:\/\/dl.acm.org\/citation.cfm?doid=366622.366644\"><em>Algorithm 64: Quicksort<\/em><\/a>, Communications of the ACM, Volume 4, Number 7, 1961-07.<\/li>\n<li>Hui, Roger K.W., and Kenneth E. Iverson, <a href=\"http:\/\/www.jsoftware.com\/help\/dictionary\/cif.htm\"><em>J Introduction and Dictionary<\/em><\/a>, 1991-2014; <code class=\"language-apl\">if.<\/code> entry.<\/li>\n<li>Hui, Roger K.W., <a href=\"http:\/\/www.jsoftware.com\/jwiki\/Essays\/Quicksort\"><em>Quicksort<\/em><\/a>, J Wiki Essay, 2005-09-28.<\/li>\n<li>Hui, Roger K.W. <a href=\"http:\/\/www.jsoftware.com\/papers\/amuse-bouches.htm#7\"><em>Sixteen APL Amuse-Bouches<\/em><\/a>, 2014-11-02.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Quicksort is a classic sorting algorithm invented by C.A.R. Hoare in 1961 [0, 1]. It has been known for some time that quicksort has a terse rendition in APL [2]. To get right to it, here is the code: Q\u2190{1\u2265\u2262\u2375:\u2375 &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2014\/12\/quicksort-in-apl\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1666","post","type-post","status-publish","format-standard","hentry","category-apl-language"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=1666"}],"version-history":[{"count":18,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1666\/revisions"}],"predecessor-version":[{"id":1830,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1666\/revisions\/1830"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=1666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=1666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=1666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}