{"id":317,"date":"2009-05-31T01:59:02","date_gmt":"2009-05-31T06:59:02","guid":{"rendered":"http:\/\/vgable.com\/blog\/2009\/05\/31\/the-best-quicksort-ever\/"},"modified":"2009-05-31T01:59:05","modified_gmt":"2009-05-31T06:59:05","slug":"the-best-quicksort-ever","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2009\/05\/31\/the-best-quicksort-ever\/","title":{"rendered":"The Best Quicksort Ever"},"content":{"rendered":"<p>The first time I saw <a href=\"http:\/\/www.haskell.org\/haskellwiki\/Introduction#Ease_of_understanding\">this quicksort in Haskell<\/a> was an eye opening moment,<\/p>\n<blockquote>\n<pre> \nqsort []     = []\nqsort (x:xs) = qsort (filter (&lt; x) xs) ++ [x] ++ qsort (filter (&gt;= x) xs)\n<\/pre>\n<p>The first line reads: &#8220;When you sort an empty list (<code>[]<\/code>), the result is another empty list&#8221;. The second line reads: &#8220;To sort a list whose first element is named <code>x<\/code> and the rest of which is named <code>xs<\/code>, sort the elements of <code>xs<\/code> that are less than <code>x<\/code>, sort the elements of <code>xs<\/code> that are greater than or equal to <code>x<\/code>, and concatenate (<code>++<\/code>) the results, with <code>x<\/code> sandwiched in the middle.&#8221;\n<\/p><\/blockquote>\n<p>The code is so concise, yet clear (even with cryptic variable names like <code>xs<\/code>). The day my professor wrote it on the whiteboard was the first time I internalized that there might be something good about the <em>alien<\/em> world of functional programming.<\/p>\n<p>The biggest revelation to me was, <code>filter (&lt; x) xs<\/code> . It&#8217;s amazing that <code>(&lt; x)<\/code> builds a temporary, unnamed, function equivalent to C++,<\/p>\n<pre>bool lessThanX(AnyOrderedType y){\n    return y &lt; X;\n}\n\/\/plus the definition of X somewhere...<\/pre>\n<p><em>Building a function!<\/em> It&#8217;s still profound stuff to me. And <strong>clearly it really uses fewer lines of code<\/strong>. Amazingly, using <a href=\"http:\/\/en.wikipedia.org\/wiki\/List_comprehension\">list comprehension<\/a> syntax is not more concise,<\/p>\n<pre>qsort (x:xs) = qsort [i | i &lt;- xs, i &lt; x] ++ [x] ++ qsort [i | i &lt;- xs, i &lt;= x]<\/pre>\n<p>compared to the original,<\/p>\n<pre>qsort (x:xs) = qsort (filter (&lt; x) xs) ++ [x] ++ qsort (filter (&gt;= x) xs)<\/pre>\n<p>I hope this is an <em>ah-ah<\/em> moment for someone else too. I&#8217;d love to know what made you interested in functional programming; or if you know of a more elegant quicksort implementation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first time I saw this quicksort in Haskell was an eye opening moment, qsort [] = [] qsort (x:xs) = qsort (filter (&lt; x) xs) ++ [x] ++ qsort (filter (&gt;= x) xs) The first line reads: &#8220;When you sort an empty list ([]), the result is another empty list&#8221;. The second line reads: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,4,13],"tags":[432,430,326,69,431],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-design","category-programming","category-sample-code","tag-algorithms","tag-college","tag-functional-programming","tag-haskell","tag-quicksort"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=317"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/317\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}