<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Knuth &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/knuth/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Shell Sort</title>
		<link>/2012/02/27/computer-algorithms-shell-sort/</link>
		<comments>/2012/02/27/computer-algorithms-shell-sort/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 20:44:54 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[Adaptive sort]]></category>
		<category><![CDATA[Bubble sort]]></category>
		<category><![CDATA[Comb sort]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[Donald Shell]]></category>
		<category><![CDATA[Insertion sort]]></category>
		<category><![CDATA[Knuth]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Merge sort]]></category>
		<category><![CDATA[Order theory]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Pratt]]></category>
		<category><![CDATA[Quicksort]]></category>
		<category><![CDATA[Shell sort]]></category>
		<category><![CDATA[Sorting algorithms]]></category>

		<guid isPermaLink="false">/?p=2734</guid>
		<description><![CDATA[Overview Insertion sort is a great algorithm, because it&#8217;s very intuitive and it is easy to implement, but the problem is that it makes many exchanges for each &#8220;light&#8221; element in order to put it on the right place. Thus “light” elements at the end of the list may slow down the performance of insertion &#8230; <a href="/2012/02/27/computer-algorithms-shell-sort/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Shell Sort</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/03/05/computer-algorithms-merge-sort/" rel="bookmark" title="Computer Algorithms: Merge Sort">Computer Algorithms: Merge Sort </a></li>
<li><a href="/2012/02/13/computer-algorithms-insertion-sort/" rel="bookmark" title="Computer Algorithms: Insertion Sort">Computer Algorithms: Insertion Sort </a></li>
<li><a href="/2013/01/02/computer-algorithms-bucket-sort/" rel="bookmark" title="Computer Algorithms: Bucket Sort">Computer Algorithms: Bucket Sort </a></li>
<li><a href="/2012/03/19/computer-algorithms-radix-sort/" rel="bookmark" title="Computer Algorithms: Radix Sort">Computer Algorithms: Radix Sort </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Overview</h2>
<p><a href="/2012/02/13/computer-algorithms-insertion-sort/" title="Computer Algorithms: Insertion Sort">Insertion sort</a> is a great algorithm, because it&#8217;s very intuitive and it is easy to implement, but the problem is that it makes many exchanges for each &#8220;light&#8221; element in order to put it on the right place. Thus “light” elements at the end of the list may slow down the performance of insertion sort a lot. That is why in 1959 <a href="http://en.wikipedia.org/wiki/Donald_Shell" title="Donald Shell" target="_blank">Donald Shell</a> proposed an algorithm that tries to overcome this problem by comparing items of the list that lie far apart.</p>
<figure id="attachment_2796" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/02/Insertion-Sort-vs.-Shell-Sort.png"><img src="/wp-content/uploads/2012/02/Insertion-Sort-vs.-Shell-Sort.png" alt="Insertion Sort vs. Shell Sort" title="Insertion Sort vs. Shell Sort" width="620" class="size-full wp-image-2796" srcset="/wp-content/uploads/2012/02/Insertion-Sort-vs.-Shell-Sort.png 640w, /wp-content/uploads/2012/02/Insertion-Sort-vs.-Shell-Sort-300x170.png 300w" sizes="(max-width: 640px) 100vw, 640px" /></a><figcaption class="wp-caption-text">Insertion sort compares every single item with all the rest elements of the list in order to find its place, while Shell sort compares items that lie far apart. This makes light elements to move faster to the front of the list.</figcaption></figure>
<p>In the other hand it is obvious that by comparing items that lie apart the list can’t be sorted in one pass as insertion sort. That is why on each pass we should use a fixed gap between the items, then decrease the value on every consecutive iteration.<span id="more-2734"></span></p>
<figure id="attachment_2791" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/02/Shell-Sort.png"><img src="/wp-content/uploads/2012/02/Shell-Sort.png" alt="Shell Sort" title="Shell Sort" width="620" class="size-full wp-image-2791" srcset="/wp-content/uploads/2012/02/Shell-Sort.png 631w, /wp-content/uploads/2012/02/Shell-Sort-190x300.png 190w" sizes="(max-width: 631px) 100vw, 631px" /></a><figcaption class="wp-caption-text">We start to compare items with a fixed gap, that becomes lesser on each iteration until it gets to 1.</figcaption></figure>
<p>However it is intuitively clear that Shell sort may need even more comparisons than insertion sort. Then why should we use it? </p>
<p>The thing is that insertion sort is not an effective sorting algorithm at all, but in some cases, when the list is almost sorted it can be quite useful. Here’s the answer of the question above. With Shell sort once the list is sorted for gap = i, it is sorted for every gap = j, where j < i, and this is its main advantage.

[caption id="attachment_2792" align="alignnone" width="620" caption="Shell sort can make less exchanges than insertion sort."]<a href="/wp-content/uploads/2012/02/Shell-Sort-Principles.png"><img src="/wp-content/uploads/2012/02/Shell-Sort-Principles.png" alt="Shell Sort Principles" title="Shell Sort Principles" width="620" class="size-full wp-image-2792" srcset="/wp-content/uploads/2012/02/Shell-Sort-Principles.png 641w, /wp-content/uploads/2012/02/Shell-Sort-Principles-150x150.png 150w, /wp-content/uploads/2012/02/Shell-Sort-Principles-300x298.png 300w" sizes="(max-width: 641px) 100vw, 641px" /></a>[/caption]</p>
<h3>How to choose gap size</h3>
<p>Not a cool thing about Shell sort is that we’ve to choose “the perfect” gap sequence for our list. However this is not an easy task, because it depends a lot of the input data. The good news is that there are some gap sequences proved to be working well in the general cases.</p>
<h3>Shell Sequence</h3>
<p>Donald Shell proposes a sequence that follows the formula FLOOR(N/2<sup>k</sup>), then for N = 1000, we get the following sequence: [500, 250, 125, 62, 31, 15, 7, 3, 1]</p>
<figure id="attachment_2793" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/02/Shell-Gap-Sequence.png"><img src="/wp-content/uploads/2012/02/Shell-Gap-Sequence.png" alt="Shell Gap Sequence" title="Shell Gap Sequence" width="620" class="size-full wp-image-2793" srcset="/wp-content/uploads/2012/02/Shell-Gap-Sequence.png 640w, /wp-content/uploads/2012/02/Shell-Gap-Sequence-300x185.png 300w" sizes="(max-width: 640px) 100vw, 640px" /></a><figcaption class="wp-caption-text">Shell sequence for N=1000: (500, 250, 125, 62, 31, 15, 7, 3, 1)</figcaption></figure>
<h3>Pratt Sequence</h3>
<p><a href="http://en.wikipedia.org/wiki/Vaughan_Ronald_Pratt" title="Vaughan Pratt" target="_blank">Pratt</a> proposes another sequence that’s growing with a slower pace than the Shell’s sequence. He proposes successive numbers of the form 2<sup>p</sup>3<sup>q</sup> or [1, 2, 3, 4, 6, 8, 9, 12, &#8230;].</p>
<figure id="attachment_2794" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/02/Pratt-Gap-Sequence.png"><img src="/wp-content/uploads/2012/02/Pratt-Gap-Sequence.png" alt="Pratt Gap Sequence" title="Pratt Gap Sequence" width="620" class="size-full wp-image-2794" srcset="/wp-content/uploads/2012/02/Pratt-Gap-Sequence.png 640w, /wp-content/uploads/2012/02/Pratt-Gap-Sequence-300x185.png 300w" sizes="(max-width: 640px) 100vw, 640px" /></a><figcaption class="wp-caption-text">Pratt sequence: (1, 2, 3, 4, 6, 8, 9, 12, ...)</figcaption></figure>
<h3>Knuth Sequence</h3>
<p>Knuth in other hand proposes his own sequence following the formula (3<sup>k</sup> &#8211; 1) / 2 or [1, 4, 14, 40, 121, &#8230;]</p>
<figure id="attachment_2795" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/02/Knuth-Gap-Sequence.png"><img src="/wp-content/uploads/2012/02/Knuth-Gap-Sequence.png" alt="Knuth Gap Sequence" title="Knuth Gap Sequence" width="620" class="size-full wp-image-2795" srcset="/wp-content/uploads/2012/02/Knuth-Gap-Sequence.png 640w, /wp-content/uploads/2012/02/Knuth-Gap-Sequence-300x185.png 300w" sizes="(max-width: 640px) 100vw, 640px" /></a><figcaption class="wp-caption-text">Knuth sequence: (1, 4, 13, 40, 121, ...)</figcaption></figure>
<p>Of course there are many other gap sequences, proposed by various developers and researchers, but the problem is that the effectiveness of the algorithm strongly depends on the input data. But before taking a look to the complexity of Shell sort, let’s see first its implementation.</p>
<h2>Implementation</h2>
<p>Here’s a Shell sort implementation on <a href="/category/php/" title="PHP on stoimen.com">PHP</a> using the Pratt gap sequence. The thing is that for this data set other gap sequences may appear to be better solution.</p>
<pre lang="php">
$input = array(6, 5, 3, 1, 8, 7, 2, 4);

function shell_sort($arr)
{
	$gaps = array(1, 2, 3, 4, 6);
	$gap  = array_pop($gaps);
	$len  = count($arr);
	
	while($gap > 0)
	{
		for($i = $gap; $i < $len; $i++) {
			
			$temp = $arr[$i];
			$j = $i;
			
			while($j >= $gap && $arr[$j - $gap] > $temp) {
				$arr[$j] = $arr[$j - $gap];
				$j -= $gap;
			}
			
			$arr[$j] = $temp;
		}
		
		$gap = array_pop($gaps);
	}
	
	return $arr;
}

// 1, 2, 3, 4, 5, 6, 7, 8
shell_sort($input);
</pre>
<p>It&#8217;s easy to change this code in order to work with Shell sequence.</p>
<pre lang="PHP">
$input = array(6, 5, 3, 1, 8, 7, 2, 4);

function shell_sort($arr)
{
        $len  = count($arr);
	$gap  = floor($len/2);
	
	while($gap > 0)
	{
		for($i = $gap; $i < $len; $i++) {
			
			$temp = $arr[$i];
			$j = $i;
			
			while($j >= $gap && $arr[$j - $gap] > $temp) {
				$arr[$j] = $arr[$j - $gap];
				$j -= $gap;
			}
			
			$arr[$j] = $temp;
		}
		
		$gap = floor($gap/2);
	}
	
	return $arr;
}

// 1, 2, 3, 4, 5, 6, 7, 8
shell_sort($input);
</pre>
<h2>Complexity</h2>
<p>Yet again we can’t determine the exact complexity of this algorithm, because it depends on the gap sequence. However we may say what is the complexity of Shell sort with the sequences of Knuth, Pratt and Donald Shell. For the Shell&#8217;s sequence the complexity is O(n<sup>2</sup>), while for the Pratt’s sequence it is O(n*log<sup>2</sup>(n)). The best approach is the Knuth sequence where the complexity is O(n<sup>3/2</sup>), as you can see on the diagram bellow.</p>
<figure id="attachment_2797" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/02/Complexity-of-Shell-Sort.png"><img src="/wp-content/uploads/2012/02/Complexity-of-Shell-Sort.png" alt="Complexity of Shell Sort" title="Complexity of Shell Sort" width="620" class="size-full wp-image-2797" srcset="/wp-content/uploads/2012/02/Complexity-of-Shell-Sort.png 640w, /wp-content/uploads/2012/02/Complexity-of-Shell-Sort-300x185.png 300w" sizes="(max-width: 640px) 100vw, 640px" /></a><figcaption class="wp-caption-text">Complexity of Shell sort with different gap sequences.</figcaption></figure>
<h2>Application</h2>
<p>Well, as insertion sort and bubble sort, Shell sort is not very effective compared to quicksort or merge sort. The good thing is that it is quite easy to implement (not easier than insertion sort), but in general it should be avoided for large data sets. Perhaps the main advantage of Shell sort is that the list can be sorted for a gap greater than 1 and thus making less exchanges than insertion sort. </p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/03/05/computer-algorithms-merge-sort/" rel="bookmark" title="Computer Algorithms: Merge Sort">Computer Algorithms: Merge Sort </a></li>
<li><a href="/2012/02/13/computer-algorithms-insertion-sort/" rel="bookmark" title="Computer Algorithms: Insertion Sort">Computer Algorithms: Insertion Sort </a></li>
<li><a href="/2013/01/02/computer-algorithms-bucket-sort/" rel="bookmark" title="Computer Algorithms: Bucket Sort">Computer Algorithms: Bucket Sort </a></li>
<li><a href="/2012/03/19/computer-algorithms-radix-sort/" rel="bookmark" title="Computer Algorithms: Radix Sort">Computer Algorithms: Radix Sort </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/02/27/computer-algorithms-shell-sort/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
