<?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>排序算法 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Tue, 28 Apr 2020 02:08:04 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] Shell sort（希尔排序）笔记</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-shell-sort%ef%bc%88%e5%b8%8c%e5%b0%94%e6%8e%92%e5%ba%8f%ef%bc%89%e7%ac%94%e8%ae%b0/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-shell-sort%ef%bc%88%e5%b8%8c%e5%b0%94%e6%8e%92%e5%ba%8f%ef%bc%89%e7%ac%94%e8%ae%b0/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sat, 13 Sep 2014 17:26:04 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Shell sort]]></category>
		<category><![CDATA[希尔排序]]></category>
		<category><![CDATA[排序算法]]></category>
		<category><![CDATA[插入排序]]></category>
		<category><![CDATA[递减增量排序]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8297</guid>

					<description><![CDATA[<p>
<span style="background-color:#00ff00;">『1』</span>概述<br />
<a href="http://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">希尔排序（Shell sort）</span></a>是一种不常用的排序算法，因为它效率不算高，但是作为插入排序的改进算法之一，有必要了解一下。</p>
<ul>
<li>
		时间复杂度：</li>
</ul>
<p>最坏情况： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_5b385f1bc0c03f1d809880eeedc67964.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="O\left( {{n^2}} \right)" /></span><script type='math/tex'>O\left( {{n^2}} \right)</script> <br />
最好情况： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_27f822e59457e53a597016fe4429ce69.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="O\left( {n{{\log }^2}n} \right)" /></span><script type='math/tex'>O\left( {n{{\log }^2}n} \right)</script> <br />
平均情况： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_13e49d1dcff43abde99a024b2387afba.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="O\left( {{n^{1.5}}} \right)" /></span><script type='math/tex'>O\left( {{n^{1.5}}} \right)</script> </p>
<ul>
<li>
		是不是稳定排序算法：否</li>
<li>
		得名起源：1959年的时候Donald Shell发明的，所以叫Shell sort</li>
</ul>
<p><span id="more-8297"></span><br />
<span style="background-color:#00ff00;">『2』</span>原理<br />
首先，希尔排序是插入排序的改进版本。因此有必要回顾一下插入排序的做法：我们知道，插入排序总是把待排序的数组分割成两部分，其中第一部分有序，第二部分无序。有序部分一开始只包含数组中的第一个元素，然后取出无序部分的第一个元素，从有序部分的最后一个元素开始，与有序部分的元素相比，从而将其插入有序部分的正确位置。<br />
这也就导致了，如果一个元素距离它的正确位置非常远（例如 <span style="color:#0000ff;">9 5 6 7 2 4 3 8 1</span> 这个数组，1这个数本应排在第一位，但是它在排序前是排在最后一位，因此它就离正确位置非常远了），那么我们就要做很多次大小比较，才能找出其正确位置，所以Donald Shell就想，能不能让这种距离正确位置很远的元素，一次就能跨越很大的距离，而不是要做那么多次比较才能找到正确位置呢？<br />
可以的。试想一下，如果单独看数组中的<span style="color:#0000ff;">9 2 1</span>这几个元素，我们只需要花费很少的几次大小比较（因为只有3个元素），就可以用插入排序的方法，把1移动到正确的位置上。相对于整个待排序的数组来说（用插入排序把1移动到正确的位置需要很多次比较），这就相当于跨越了很大的距离。对于数组中所有的元素，我们都可以这样干，让它们都搭上&#8220;大跨步&#8221;的快车，这样就可以&#8220;迅速&#8221;地把很多元素放到了更接近正确位置的位置。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-shell-sort%ef%bc%88%e5%b8%8c%e5%b0%94%e6%8e%92%e5%ba%8f%ef%bc%89%e7%ac%94%e8%ae%b0/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
<span style="background-color:#00ff00;">『1』</span>概述<br />
<a href="http://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">希尔排序（Shell sort）</span></a>是一种不常用的排序算法，因为它效率不算高，但是作为插入排序的改进算法之一，有必要了解一下。</p>
<ul>
<li>
		时间复杂度：</li>
</ul>
<p>最坏情况： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_5b385f1bc0c03f1d809880eeedc67964.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="O\left( {{n^2}} \right)" /></span><script type='math/tex'>O\left( {{n^2}} \right)</script> <br />
最好情况： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_27f822e59457e53a597016fe4429ce69.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="O\left( {n{{\log }^2}n} \right)" /></span><script type='math/tex'>O\left( {n{{\log }^2}n} \right)</script> <br />
平均情况： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_13e49d1dcff43abde99a024b2387afba.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="O\left( {{n^{1.5}}} \right)" /></span><script type='math/tex'>O\left( {{n^{1.5}}} \right)</script> </p>
<ul>
<li>
		是不是稳定排序算法：否</li>
<li>
		得名起源：1959年的时候Donald Shell发明的，所以叫Shell sort</li>
</ul>
<p><span id="more-8297"></span><br />
<span style="background-color:#00ff00;">『2』</span>原理<br />
首先，希尔排序是插入排序的改进版本。因此有必要回顾一下插入排序的做法：我们知道，插入排序总是把待排序的数组分割成两部分，其中第一部分有序，第二部分无序。有序部分一开始只包含数组中的第一个元素，然后取出无序部分的第一个元素，从有序部分的最后一个元素开始，与有序部分的元素相比，从而将其插入有序部分的正确位置。<br />
这也就导致了，如果一个元素距离它的正确位置非常远（例如 <span style="color:#0000ff;">9 5 6 7 2 4 3 8 1</span> 这个数组，1这个数本应排在第一位，但是它在排序前是排在最后一位，因此它就离正确位置非常远了），那么我们就要做很多次大小比较，才能找出其正确位置，所以Donald Shell就想，能不能让这种距离正确位置很远的元素，一次就能跨越很大的距离，而不是要做那么多次比较才能找到正确位置呢？<br />
可以的。试想一下，如果单独看数组中的<span style="color:#0000ff;">9 2 1</span>这几个元素，我们只需要花费很少的几次大小比较（因为只有3个元素），就可以用插入排序的方法，把1移动到正确的位置上。相对于整个待排序的数组来说（用插入排序把1移动到正确的位置需要很多次比较），这就相当于跨越了很大的距离。对于数组中所有的元素，我们都可以这样干，让它们都搭上&ldquo;大跨步&rdquo;的快车，这样就可以&ldquo;迅速&rdquo;地把很多元素放到了更接近正确位置的位置。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
那有人会问了，在<span style="color:#0000ff;">9 2 1</span>这几个数中对1进行插入排序，确实很快，但是<span style="color: rgb(0, 0, 255);">9 2 1</span>这几个数是怎么挑选出来的呢？显然是按一定的规则从原数组中切分出来的。其实在这个例子中，就是把数组按<span style="color:#0000ff;">间距</span>4（或者称<span style="color:#0000ff;">步长</span>4）分成了几部分得到的：<br />
<span style="color: rgb(0, 0, 255);">9 5 6 7 2 4 3 8 1</span><br />
<span style="color:#ff0000;">9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br />
&nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4<br />
&nbsp; &nbsp; &nbsp; 6 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;7 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8</span><br />
那么，分别对这4个子数组（9 2 1，5 4，6 3，7 8）进行插入排序，就可以高效地把一些元素（例如前面所说的 1）放到离正确位置很近的位置上（或者刚好就放到了正确的位置上）。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
那又有人会说了，对 9 2 1 这个子数组排序后，得到了 1 2 9，1和9都恰巧处于排过序的原数组中的正确位置上了；但是2这个数就没有那么好运气了，假设数组记为a，则2应位于a[1]处，但是对 9 2 1 排序后，2实际上是位于a[4]处。对其他的子数组（5 4，6 3，7 8），也有同样的问题：我们分别把这些子数组排序后，再拼凑起来成为一个完整的数组（得到<span style="color: rgb(0, 0, 255);">1 4 3 7 2 5 6 8 9</span>）的话，并不能保证整个数组是有序的。<br />
所以只进行一次切分原数组、并分别排序子数组，再合成一个大数组，并不能解决问题。所以，我们继续按更小的<span style="color:#0000ff;">间距</span>（或称为<span style="color:#0000ff;">步长</span>），把上面得到的、部分排序的子数组&ldquo;合成&rdquo;的大数组进行切分，这一次，我们把前一次的<span style="color:#0000ff;">间距</span>4除以2，也就是间距为2：<br />
<span style="color: rgb(0, 0, 255);">1 4 3 7 2 5 6 8 9</span><br />
<span style="color:#ff0000;">1 &nbsp; &nbsp;3 &nbsp; &nbsp;2 &nbsp; &nbsp;6 &nbsp; &nbsp;9<br />
&nbsp; &nbsp;4 &nbsp; &nbsp;7 &nbsp; &nbsp;5 &nbsp; &nbsp;8</span><br />
这次再对 1 3 2 6 9，4 7 5 8 这两个子数组分别单独进行插入排序，得到 1 2 3 6 9 和 4 5 7 8，再合成为一个大数组：1 4 2 5 3 7 6 8 9。这个大数组仍然不是有序的。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
于是我们最后一次，把间距（步长）再除以2，也就是间距为1，并对大数组进行切分：<br />
<span style="color:#0000ff;">1 4 2 5 3 7 6 8 9</span><br />
<span style="color:#ff0000;">1 4 2 5 3 7 6 8 9</span><br />
由于间距已经缩到最小了，所以切分之后的子数组也就是切分之前的大数组，我们再对它进行插入排序，就一定可以得到完全有序的大数组了，至此，工作完成。<br />
由于在前面的几步切分、排序的过程中，已经把一个可能原来&ldquo;很无序&rdquo;的数组，逐步地变成了一个&ldquo;比较有序&rdquo;的数组（虽然还不是完全有序的），并且我们知道，一个数组越有序，对其进行插入排序的效率就越高，因此，在前面的努力之下，我们通过最后一步切分、排序，就可以比较轻松地完成整个数组的排序了&mdash;&mdash;正所谓&ldquo;前人种树，后人乘凉&rdquo;。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『3』</span>时间复杂度<br />
希尔排序的时间复杂度分析很复杂，如果你有兴趣，可以研读<a href="http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/shell/shellen.htm" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">这篇</span></a>文章。总之可以证明，其最坏情况下的时间复杂度是 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_5b385f1bc0c03f1d809880eeedc67964.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="O\left( {{n^2}} \right)" /></span><script type='math/tex'>O\left( {{n^2}} \right)</script> ，但是对希尔排序稍加一点改进，就可以使其时间复杂度提高到 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_13e49d1dcff43abde99a024b2387afba.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="O\left( {{n^{1.5}}} \right)" /></span><script type='math/tex'>O\left( {{n^{1.5}}} \right)</script> 。<br />
怎么改进呢？参考前面的例子，我们会发现，在对数组进行第1次切分和第2次切分的过程中，我们分别得到了两个子数组<span style="color:#ff0000;"> 9 2 1 </span>和<span style="color:#ff0000;"> 1 3 2 6 9</span>，它们里面都含有 1 2 9 这三个元素。在第一次切分数组、排序之后，1 2 9 这三个元素已经是正确的顺序了，但是在第2次对数组进行切分、排序的时候，又对这三个元素进行了大小比较，这纯粹是浪费时间，因此，如果可以避免上一轮排序中子数组的所有元素进入下一轮排序中的子数组里，我们就可以避免做很多无用功。所以，只需要在间距（步长）为偶数值的时候，将其+1，就可以得到没有公因子的间距（步长）序列，避免了上面的不利情况。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
正因为希尔排序的效率与间距（步长）有莫大的关系，因此，人们找到了各种高效的步长。如Wiki所说：</p>
<blockquote>
<p>
		已知的最好步长串行是由Sedgewick提出的 (1, 5, 19, 41, 109,...)，该串行的项来自  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_f132293b925b264a191f2f0f2304a5c8.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="9 \times {4^i} - 9 \times {2^i} + 1" /></span><script type='math/tex'>9 \times {4^i} - 9 \times {2^i} + 1</script>  和  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_20d73af1fd762e674a77b21579963998.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{4^i} - 3 \times {2^i} + 1" /></span><script type='math/tex'>{4^i} - 3 \times {2^i} + 1</script> &nbsp;这两个算式。</p>
</blockquote>
<p>其中， <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_3ebb32f0b72e488c4b85ba732bd62635.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="i = 0,1,2, \cdots " /></span><script type='math/tex'>i = 0,1,2, \cdots </script> 时，第一个式子计算出的值分别为1，19，109，&hellip;&hellip;； <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_1ce23ff29180121f879a3547ea2b08a8.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="i = 2,3, \cdots " /></span><script type='math/tex'>i = 2,3, \cdots </script> 时，第二个式子算出的值分别为5，41，&hellip;&hellip;</p>
<p style="font-size: 16px; margin: 5px 0px; clear: both; font-family: sans-serif;">
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-shell-sort%ef%bc%88%e5%b8%8c%e5%b0%94%e6%8e%92%e5%ba%8f%ef%bc%89%e7%ac%94%e8%ae%b0/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
