﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Jacky Loves C++-随笔分类-Algorithm</title><link>http://www.cppblog.com/jacky2019/category/3714.html</link><description>Next to my life, software is my passion.</description><language>zh-cn</language><lastBuildDate>Mon, 19 May 2008 15:01:30 GMT</lastBuildDate><pubDate>Mon, 19 May 2008 15:01:30 GMT</pubDate><ttl>60</ttl><item><title>求n!的结果中末尾0的个数</title><link>http://www.cppblog.com/jacky2019/archive/2007/04/19/22306.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Thu, 19 Apr 2007 07:30:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/04/19/22306.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/22306.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/04/19/22306.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/22306.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/22306.html</trackback:ping><description><![CDATA[<p><span>终于悟到了。</span></p>
<p><span>首先，很容易想到的就是，求</span>0<span>的个数，就是求</span>5<span>的个数（如果这个你都想不明白的话，那就。。。再好好想想吧，呵呵）；</span></p>
<p><span>接下来，如何求</span>5<span>的个数呢？如果遍历一遍的话，那显然是太慢了！因为这种计算题太有规律了！想了好久，终于想出来了：</span></p>
<p>Result = 0; // <span>最后的结果</span></p>
<p>while ( N &gt;= 5 )</p>
<p>{</p>
<p>&nbsp;N /= 5;</p>
<p>&nbsp;Result += N;</p>
<p>}</p>
<p>// <span>结束了。</span></p>
<p>&nbsp;</p>
<p><span>没错，就是这么简单！下面简单说说为什么这样子做是对的（偶小试了一下，没问题，呵呵）：</span></p>
<p><span>第一次除以</span>5<span>表示</span>5<span>的倍数的个数，</span></p>
<p><span>第二次除以</span>5<span>表示</span>5<span>的平方的倍数的个数，（显然，</span>5<span>的平方暗含了两个</span>0<span>）</span></p>
<p><span>。。。依此类推</span></p>
<p><span>最后当</span>N&lt;5<span>了，结束。</span></p>
<p>&nbsp;</p>
<p><span>小小的验证一下：</span></p>
<p>26<span>！</span></p>
<p>26/5 = 5<span>，</span> 5/5 = 1<span>，那么最后</span>0<span>的个数就是</span>6<span>了。用</span>Google<span>算了一下，结果</span>G<span>大叔直接用有效数字表示了，</span>@$%$%@$%<span>。。。不过应该是没错了。恩。</span></p>
<p>Sigh<span>，知道结果后才知道原来这么简单的阿，偶土了。</span></p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/22306.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-04-19 15:30 <a href="http://www.cppblog.com/jacky2019/archive/2007/04/19/22306.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个非常简单的求随机数的算法</title><link>http://www.cppblog.com/jacky2019/archive/2007/04/01/21047.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Sun, 01 Apr 2007 07:54:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/04/01/21047.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/21047.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/04/01/21047.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/21047.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/21047.html</trackback:ping><description><![CDATA[#include &lt;stdio.h&gt;<br>#include &lt;windows.h&gt;
<p>unsigned long int next = 1;<br>unsigned int rand(void) <br>{ <br>&nbsp; next = next * 1103515245 + 12345; <br>&nbsp; return (unsigned int)(next/65536) % 32768; <br>} </p>
<p>void srand(unsigned int seed) <br>{ <br>&nbsp; next = seed; <br>}</p>
<p>int main() <br>{&nbsp;<br>&nbsp;srand(GetTickCount());</p>
<p>&nbsp;for(int i=0; i&lt;10; i++)<br>&nbsp;{<br>&nbsp;&nbsp;for(int j=0; j&lt;10; j++)<br>&nbsp;&nbsp;&nbsp;printf("%d ", rand());<br>&nbsp;&nbsp;printf("\n");&nbsp;&nbsp;&nbsp;<br>&nbsp;}</p>
<p>&nbsp;return 0; <br>}</p>
<br>也可如下：<br>&nbsp;
<p><span>随机数</span></p>
<p>#include &lt;stdlib.h&gt;</p>
<p>Srand( GetTickCount() );</p>
<p>Rand();</p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/21047.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-04-01 15:54 <a href="http://www.cppblog.com/jacky2019/archive/2007/04/01/21047.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>银行家算法学习</title><link>http://www.cppblog.com/jacky2019/archive/2007/03/21/20313.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Wed, 21 Mar 2007 11:00:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/03/21/20313.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/20313.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/03/21/20313.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/20313.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/20313.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="MARGIN: 0in 0in 10pt">
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">银行家算法是著名的操作系统用来解决死锁问题的算法。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 10pt">
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">它是如何实现解决死锁问题的呢？</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 10pt">
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">今天稍微学习了一下，就稍微说一下其原理吧，免得忘了。其实原理很简单！</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 10pt">
				<font face="Calibri">
						<span style="mso-spacerun: yes">     </span>Banker algorithm</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">最重要的一点是：保证操作系统的安全状态！这也是操作系统判断是否分配给一个进程资源的标准！那什么是安全状态？举个小例子，进程</span>
				<font face="Calibri">P</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">需要申请</span>
				<font face="Calibri">8</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">个资源（假设都是一样的），已经申请了</span>
				<font face="Calibri">5</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">个资源，还差</span>
				<font face="Calibri">3</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">个资源。若这个时候操作系统还剩下</span>
				<font face="Calibri">2</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">个资源。很显然，这个时候操作系统无论如何都不能再分配资源给进程</span>
				<font face="Calibri">P</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">了，因为即使全部给了他也不够，还很可能会造成死锁。若这个时候操作系统还有</span>
				<font face="Calibri">3</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">个资源，无论</span>
				<font face="Calibri">P</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">这一次申请几个资源，操作系统都可以满足他，因为操作系统可以保证</span>
				<font face="Calibri">P</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">不死锁，只要他不把剩余的资源分配给别人，进程</span>
				<font face="Calibri">P</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">就一定能顺利完成任务。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 10pt">
				<span style="mso-spacerun: yes">
						<font face="Calibri">  </font>
				</span>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">为什么银行家算法是可行的呢？这里需要严格的证明一下。我这里就简单得说一下吧。不管任何时候，操作系统分配资源的时候都可以保证当前接受资源的进程不会陷入死锁，因为操作系统总是可以满足该进程需要的资源的。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 10pt">
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">假设有</span>
				<font face="Calibri">n</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">个进程</span>
				<font face="Calibri">{p1, p2, p3, … pn}</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">，最后一个分配到资源的是</span>
				<font face="Calibri">pi</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">，</span>
				<font face="Calibri">pi</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">还需要</span>
				<font face="Calibri">mi</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">个资源，假设此时操作系统还有</span>
				<font face="Calibri">m</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">个资源剩余。那么很显然</span>
				<font face="Calibri">m&gt;=mi</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">！而且如果之后操作系统又把资源分配给其他进程了，假设是</span>
				<font face="Calibri">pj</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">，</span>
				<font face="Calibri">pj</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">还需要</span>
				<font face="Calibri">mj</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">个资源，同理可知</span>
				<font face="Calibri">m&gt;=mj</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">！也就是说在所有的进程中，还需要的资源数总是有小于</span>
				<font face="Calibri">m</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">的！这样就可以保证资源数永远不会为</span>
				<font face="Calibri">0</font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">，即使可能暂时性为</span>
				<font face="Calibri">0 </font>
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">。另外，还需要保证资源数不会减少！而且，所有已经分配到资源的进程总有一天会归还它所拥有的资源！根据操作系统再分配的时候的状态即可判定。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 10pt">
				<span lang="ZH-CN" style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin">胡说八道了一通。。。不知有没有把问题讲明白了，还是越讲越糊涂？</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0in 0in 10pt">
				<font face="Calibri">GL &amp; HF</font>
		</p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/20313.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-03-21 19:00 <a href="http://www.cppblog.com/jacky2019/archive/2007/03/21/20313.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>合并排序算法</title><link>http://www.cppblog.com/jacky2019/archive/2007/03/18/20101.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Sun, 18 Mar 2007 14:33:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/03/18/20101.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/20101.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/03/18/20101.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/20101.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/20101.html</trackback:ping><description><![CDATA[		#include &lt;iostream&gt;<br />#include &lt;string&gt;<br />using namespace std;<br /><br />void Merge(int * A, int p, int q, int r);<br /><br />void output(int * A, int size)<br />{<br />    for(int i=0; i&lt;size; i++)<br />        cout &lt;&lt; A[i] &lt;&lt; " ";<br />    <br />    cout &lt;&lt; endl;<br />}<br /><br />void MergeSort(int * A, int p, int r) //sort A[p,r]<br />{<br />    if(p &lt; r)<br />    {<br />        int q = (p+r)/2;<br />        MergeSort(A, p, q);<br />        MergeSort(A, q+1, r);<br />        Merge(A, p, q, r);<br />    }<br />}<br /><br />void Merge(int * A, int p, int q, int r) //merge A[p,q] with A[q+1,r]<br />{<br />    // array1 A[p,q]<br />    // array2 A[q+1,r]<br />    int * pp = new int[r-p+1];<br />    int i1=p;<br />    int i2=q+1;<br />    for(int i=0; i&lt;r-p+1; i++)<br />    {<br />        if(i1&lt;=q &amp;&amp; i2&lt;=r)<br />        {<br />            if(A[i1]&lt;A[i2])<br />            {<br />                pp[i] = A[i1];<br />                i1++;<br />            }<br />            else<br />            {<br />                pp[i] = A[i2];<br />                i2++;<br />            }<br />        }<br />        else if(i1&gt;q)<br />        {<br />            for(;i&lt;r-p+1;i++)<br />            {<br />                pp[i] = A[i2];<br />                i2++;<br />            }<br />            <br />            break;<br />        }<br />        else if(i2&gt;r)<br />        {<br />            for(;i&lt;r-p+1;i++)<br />            {<br />                pp[i] = A[i1];<br />                i1++;<br />            }<br />            <br />            break;<br />        }<br />    }<br />    <br />    for(int i=p; i&lt;=r; i++)<br />    {<br />        int t = pp[i-p];<br />        A[i] = pp[i-p];<br />    }<br />    <br />    delete [] pp;<br />}<br /><br /><br />int main()<br />{<br />    int Ar[] = {11,2,9,7,6,5,3,8,2,3,5,1,-5,8,7};    <br />    int size = sizeof(Ar)/sizeof(int);<br />    output(Ar, size);<br />    <br />    MergeSort(Ar,0,size-1);    <br />    output(Ar, size);    <br /><br />    return 0;<br />}<img src ="http://www.cppblog.com/jacky2019/aggbug/20101.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-03-18 22:33 <a href="http://www.cppblog.com/jacky2019/archive/2007/03/18/20101.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>希尔排序法 Shell Sort</title><link>http://www.cppblog.com/jacky2019/archive/2007/03/07/19340.html</link><dc:creator>小熊</dc:creator><author>小熊</author><pubDate>Wed, 07 Mar 2007 02:19:00 GMT</pubDate><guid>http://www.cppblog.com/jacky2019/archive/2007/03/07/19340.html</guid><wfw:comment>http://www.cppblog.com/jacky2019/comments/19340.html</wfw:comment><comments>http://www.cppblog.com/jacky2019/archive/2007/03/07/19340.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jacky2019/comments/commentRss/19340.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jacky2019/services/trackbacks/19340.html</trackback:ping><description><![CDATA[
		<p>#include &lt;iostream&gt;<br />using namespace std;</p>
		<p>template &lt;class T&gt;<br />void ShellSort(T a[], int N)<br />{<br /> for (int gap = N/2; gap; gap = gap/2)<br /> {<br />  for (int i = gap; i &lt; N; i++)<br />  {<br />   T temp = a[i];<br />   int j;<br />   for (j = i; j &gt;= gap &amp;&amp; temp &lt; a[j - gap]; j -= gap)<br />   { <br />    a[j] = a[j - gap];<br />   }<br />   a[j] = temp;<br />  }<br /> }<br />} </p>
		<p>template &lt;class T&gt;<br />void output(T a[], int N)<br />{<br /> for(int i=0; i&lt;N; i++)<br /> {<br />  cout &lt;&lt; i &lt;&lt; ":" &lt;&lt; a[i] &lt;&lt; endl;<br /> }<br />}</p>
		<p>int main()<br />{<br /> int a[] = {3,3,1,0,2,2,1,9,8,3,1,1,2,6,1,2,7,8};<br /> output(a, sizeof(a)/sizeof(int));<br /> ShellSort(a, sizeof(a)/sizeof(int));<br /> output(a, sizeof(a)/sizeof(int));<br /> <br /> //end:<br /> int end;<br /> cin &gt;&gt; end;<br /> return 0;<br />}<br /></p>
<img src ="http://www.cppblog.com/jacky2019/aggbug/19340.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jacky2019/" target="_blank">小熊</a> 2007-03-07 10:19 <a href="http://www.cppblog.com/jacky2019/archive/2007/03/07/19340.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>