{"id":21115,"date":"2013-09-18T05:52:35","date_gmt":"2013-09-18T10:52:35","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=21115"},"modified":"2013-09-18T14:51:12","modified_gmt":"2013-09-18T19:51:12","slug":"15-sorting-algorithms-in-6-minutes","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=21115","title":{"rendered":"15 Sorting Algorithms in 6 Minutes"},"content":{"rendered":"<p>I don&#8217;t have a new entry for Good Robot done, so I&#8217;m going to resort to the crutch of delinquent bloggers everywhere and post a YouTube video made by someone else and use it as a conversation-starter.<\/p>\n<p>Computers do a <strong>lot<\/strong> of sorting. Alphabetizing names, sorting enemies by distance, arranging a list of polygons from far to near, putting a high score table in order, arranging files according to size or date, ordering search results by word frequency, and a million other things.  There are a lot of different ways of sorting lists of values.Some clever person wrote a program to show the sorting take place and emit audio based on the current state of the list.  I find the result to be kind of hypnotic:<\/p>\n<p><table class='nomargin' cellspacing='0' width='100%' cellpadding='0' align='center' border='0'><tr><td><iframe loading=\"lazy\" width=\"1024\" height=\"576\" src=\"https:\/\/www.youtube.com\/embed\/kPRA0W1kECg\" frameborder=\"0\" allowfullscreen class=\"embed\"><\/iframe><br\/><small><a href='http:\/\/www.youtube.com\/watch?v=kPRA0W1kECg'>Link (YouTube)<\/a><\/small><\/td><\/tr><\/table><\/p>\n<p><em>Shamus, why are there so many different sort algorithms?<\/em><\/p>\n<p>Good question, me. The reason is that there are a lot of different sort <em>problems<\/em>. The above sorts look like they&#8217;re sorting some really predictable data: A list of all the numbers from 1 to 1,000, or whatever. This is not always the case. Sometimes it is <strong>very<\/strong> not the case.<\/p>\n<p><!--more-->Back in the old days sort was depicted as being a tradeoff between speed and memory usage. If you&#8217;ve got lots of memory (or the data is small) then you can create space for a copy of the stuff you&#8217;re sorting and fill in the new space as you sort. If you don&#8217;t, then you have to move the entries around in-place.  It&#8217;s like re-arranging a room.  It&#8217;s easy if you&#8217;ve got another room you can shove some items into, and a lot harder if you have to keep all the furniture in the room as you work. A lot of sorts are optimized for different levels of memory use.  Maybe one is slow but only requires room for 1 additional item. Another is faster but requires enough memory to contain half of the list. Another is faster still but requires a complete copy of the list, thus doubling memory usage. <\/p>\n<p>Likewise, optimal techniques will vary based on what you&#8217;re sorting.  If I&#8217;m sorting the integers from 1 to 100 and I&#8217;ve got a bunch of instances of the number 10 next to each other, I know those values are sorted relative to each other and don&#8217;t need to be fussed with.  If I find another 10, I can stick it before or after these other tens and it doesn&#8217;t matter.  But if I&#8217;m sorting floating-point numbers like 3.285714285714286 and 12.83333333333333 then I&#8217;m probably not going to have large blocks of same-value entries next to each other. Also, I think some sorts make guesses about where items appear in the list based on average or median values.  That&#8217;s great if you can say, &#8220;These values all must fall within the range of 1-100, so I know this value of 80 will be somewhere near the top.&#8221;  But if your list is (for whatever reason) all the digits from 1 to 1,000 but also there is a value of ten billion stuck in there, then assumptions about ranges or averages could really make a mess of things.<\/p>\n<p>Then there are concerns about how scrambled the list might be.  If you begin with the assumption that the list is complete chaos at the outset, then you might use something optimized for really complex sorting.  But what if the list is only chaos sometimes, but the majority of the time it&#8217;s in perfect order with just one item out of place? Going back to our furniture-moving analogy: It&#8217;s overkill to shove all the furniture into the spare room if all you want to do is move the end-table. <\/p>\n<p>Maybe you&#8217;ve got a data set that&#8217;s too massive to fit in memory, so you need a sort that that&#8217;s good at operating on isolated segments. Maybe the entries are massive and moving them takes a lot of time (maybe you need to write to disk every time you move something) so you want a system that might spend more CPU time thinking so it can do the sort in the fewest possible moves.<\/p>\n<p>And let us not forget the all important cost: Programmer time. If you&#8217;re never doing more than occasionally sorting a couple hundred small items, then you should not, under any circumstances, worry, argue, or even <em>think<\/em> about what sort to use. Just use whatever sort is handy, and if you don&#8217;t have one handy just use stupid bubble sort. Programmer time is precious and your brainpower is probably needed elsewhere. <\/p>\n<p>I say this knowing nobody will take my advice because getting things <em>just right<\/em> is part of the fun of this job.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t have a new entry for Good Robot done, so I&#8217;m going to resort to the crutch of delinquent bloggers everywhere and post a YouTube video made by someone else and use it as a conversation-starter. Computers do a lot of sorting. Alphabetizing names, sorting enemies by distance, arranging a list of polygons from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-21115","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/21115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21115"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/21115\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}