<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>unplugged on C'est la Z</title><link>https://cestlaz.github.io/cestlaz-old-hugo.github.io/tags/unplugged/</link><description>C'est la Z (unplugged)</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 26 Jan 2020 13:10:47 -0400</lastBuildDate><atom:link href="https://cestlaz.github.io/cestlaz-old-hugo.github.io/tags/unplugged/rss.xml" rel="self" type="application/rss"/><item><title>Sorting by hand or searching and inserting</title><link>https://cestlaz.github.io/cestlaz-old-hugo.github.io/post/sorting-by-hand/</link><pubDate>Sun, 26 Jan 2020 13:10:47 -0400</pubDate><guid>https://cestlaz.github.io/cestlaz-old-hugo.github.io/post/sorting-by-hand/</guid><description>&lt;script type="text/javascript"
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
&lt;/script>
&lt;p>
Natan commenting on work the other day:&lt;/p>
&lt;blockquote>
"You have two lists. One is sorted, the other is not. Every item in
one list corresponds to an item in the other. Is it faster to sort the
unsorted list and then merge them, or simply go through the unsorted
list in order and pair each item with the item you can find in the
presorted list?" is a question I asked today in the dressing room of
an off-Broadway theater. I've used RegEx at my job before, but I never
thought Big O notation would become relevant!
Let this be submitted as evidence that a computer science education is
valuable no matter what field you go into. That, or I'm just a
particular kind of crazy.
&lt;/blockquote>
&lt;p>
There&amp;#39;s clearly an unplugged activity here :-)&lt;/p>
&lt;p>
As a programming exercise, it&amp;#39;s pretty easy to analyze. Since one list
is sorted, you can use a binary search for each insertion. That&amp;#39;s \( n \)
searches of \(lgn\) each yields an \( nlogn \) run time. Of course,
if the sorted list is an array and you have to shift all the elements
down in linear time for each insert, that changes things.&lt;/p>
&lt;p>
Sorting first is also \(nlogn\). Use an \(nlogn\) sort and then merge
the two lists which is linear. Here though, you will also either have
to ultimately do the shifting for insertions or use extra space by
making an new array or list for the two merged sets.&lt;/p>
&lt;p>
The interesting discussion points here are the issues like in place
vs not and the extra time needed for the shifts.&lt;/p>
&lt;p>
Unplugged though allows for even richer discussion.&lt;/p>
&lt;p>
Having humans step through computer algorithms can be popular
activities at times and they can also be very instructive but as
humans, we&amp;#39;re not optimized the same ways computers are. While we
might perform some tasks using set algorithms and those algorithms
might be the same as on a computer, more often than not, they&amp;#39;re
different - at least at a higher thought level.&lt;/p>
&lt;p>
Let&amp;#39;s take the insertion solution to our problem. Code wise, we&amp;#39;d use
a binary search. Humans however don&amp;#39;t use binary search. If we did, it
would actually be pretty inefficient. We&amp;#39;d have to locate the exact
middle which would probably require specific counting and already
we&amp;#39;ve used linear time. Even if we guessed the first middle correctly,
each additional split would require we find a new middle and again,
realistically, each time we&amp;#39;d count - more linear components.&lt;/p>
&lt;p>
In reality, we&amp;#39;d probably use a guided type search. The specific
problem involved inserting letters from an unsorted pile into
envelopes in a sorted pile so we&amp;#39;d jump down in the envelope pile by a
guess amount based on the letter we&amp;#39;re looking for - Last name Tucker,
we&amp;#39;d jump close to the end. For Davis, near the front. If we ended up
far off, we&amp;#39;d jump again, if not, we&amp;#39;d rifle through one by one for
the insertion point. There are similarities to a binary search but
it&amp;#39;s a very different algorithm. We couldn&amp;#39;t easily code up our guided
search as we have an intuitive instant ability to decide where to jump
to and when to jump but on the other hand we&amp;#39;re slower if we use the
proven \(logn\) binary search.&lt;/p>
&lt;p>
Sorting is similar. On the computer we&amp;#39;d probably use our langauges
built in sort which would be \(nlogn\). By hand, we&amp;#39;d probably use
some sort of bucket sort with merging in the individual piles. Even if
we were sorting continuous values as opposed to discrete ones like
names, we&amp;#39;d probably use buckets. &lt;/p>
&lt;p>
All of this can lead to very rich discussion - how we do things vs how
the computer does and even more, how we decide how to do things. Maybe
with a small data set we don&amp;#39;t even do a bucket sort but more of an
insertion or selection type sort. How do we decide what our jumps our
when we&amp;#39;re searching by hand or our bucket demarcations when sorting?&lt;/p>
&lt;p>
Lots of good stuff here, all because somebody at Natan&amp;#39;s job forgot to
sort the letters before they printed them out.&lt;/p></description></item></channel></rss>