{"id":3327,"date":"2016-02-29T11:46:13","date_gmt":"2016-02-29T11:46:13","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=3327"},"modified":"2016-03-01T07:41:54","modified_gmt":"2016-03-01T07:41:54","slug":"quicksort-in-apl-revisited","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2016\/02\/quicksort-in-apl-revisited\/","title":{"rendered":"Quicksort in APL Revisited"},"content":{"rendered":"<p>A <a href=\"http:\/\/www.dyalog.com\/forum\/viewtopic.php?f=12&amp;t=979#p3867\" target=\"_blank\">message in the Forum<\/a> inquired on sorting strings in APL with a custom comparison function.<\/p>\n<p>First, sorting strings <em>without<\/em> a custom comparison function obtains with a terse expression:<\/p>\n<pre><code class=\" language-apl\">      {\u2375[\u234b\u2191\u2375]} 'syzygy' 'boustrophedon' 'kakistocracy' 'chthonic'\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502boustrophedon\u2502chthonic\u2502kakistocracy\u2502syzygy\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>Sorting strings <em>with<\/em> a custom comparison function can also be accomplished succinctly by simple modifications to the Quicksort function<\/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}\r\n<\/code><\/pre>\n<p>in a <a href=\"http:\/\/www.dyalog.com\/blog\/2014\/12\/quicksort-in-apl\/\" target=\"_blank\">Dyalog blog post in 2014<\/a>. A version that accepts a comparison function operand is:<\/p>\n<pre><code class=\" language-apl\">QS\u2190{1\u2265\u2262\u2375:\u2375 \u22c4 (\u2207 \u2375\u233f\u23680&gt;s),(\u2375\u233f\u23680=s),\u2207 \u2375\u233f\u23680&lt;s\u2190\u2375 \u237a\u237a\u00a8 \u2375\u2337\u2368?\u2262\u2375}\r\n<\/code><\/pre>\n<p>The operand function <code class=\" language-apl\">\u237a \u237a\u237a \u2375<\/code> is required to return <code class=\" language-apl\">\u00af1<\/code>, <code class=\" language-apl\">0<\/code>, or <code class=\" language-apl\">1<\/code>, according to whether <code class=\" language-apl\">\u237a<\/code> is less than, equal to, or greater than <code class=\" language-apl\">\u2375<\/code>. In <code class=\" language-apl\">QS<\/code>, the phrase <code class=\" language-apl\">s\u2190\u2375 \u237a\u237a\u00a8 \u2375\u2337\u2368?\u2262\u2375<\/code> compares each element of <code class=\" language-apl\">\u2375<\/code> against a randomly chosen pivot <code class=\" language-apl\">\u2375\u2337\u2368?\u2262\u2375<\/code>; the function then applies recursively to the elements of <code class=\" language-apl\">\u2375<\/code> which are less than the pivot <code class=\" language-apl\">(0&gt;s)<\/code> and those which are greater than the pivot <code class=\" language-apl\">(0&lt;s)<\/code>.<\/p>\n<p>Example with numbers:<\/p>\n<pre><code class=\" language-apl\">      (\u00d7-)QS 3 1 4 1 5 9 2 6 5 3 5 8 97 9\r\n1 1 2 3 3 4 5 5 5 6 8 9 9 97\r\n\r\n      3(\u00d7-)4\r\n\u00af1\r\n      3(\u00d7-)3\r\n0\r\n      3(\u00d7-)\u00af17\r\n1\r\n<\/code><\/pre>\n<p>Example with strings:<\/p>\n<pre><code class=\" language-apl\">      strcmp\u2190{\u237a\u2261\u2375:0 \u22c4 \u00af1*&lt;\/\u234b\u2191\u237a \u2375}\r\n\r\n      'syzygy' strcmp 'syzygy'\r\n0\r\n      'syzygy' strcmp 'eleemosynary'\r\n1\r\n      'syzygy' strcmp 'zumba'\r\n\u00af1\r\n\r\n      strcmp QS 'syzygy' 'boustrophedon' 'kakistocracy' 'chthonic'\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502boustrophedon\u2502chthonic\u2502kakistocracy\u2502syzygy\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>A more efficient string comparison function is left as an exercise for the reader. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A message in the Forum inquired on sorting strings in APL with a custom comparison function. First, sorting strings without a custom comparison function obtains with a terse expression: {\u2375[\u234b\u2191\u2375]} &#8216;syzygy&#8217; &#8216;boustrophedon&#8217; &#8216;kakistocracy&#8217; &#8216;chthonic&#8217; \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502boustrophedon\u2502chthonic\u2502kakistocracy\u2502syzygy\u2502 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2518 Sorting strings with &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2016\/02\/quicksort-in-apl-revisited\/\">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":[13,8,18],"tags":[],"class_list":["post-3327","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-apl-language","category-tips-tricks"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/3327","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=3327"}],"version-history":[{"count":6,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/3327\/revisions"}],"predecessor-version":[{"id":3779,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/3327\/revisions\/3779"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=3327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=3327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=3327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}