﻿<?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++博客-只有面对现实，你才能超越现实-随笔分类-算法</title><link>http://www.cppblog.com/wanghaiguang/category/19356.html</link><description>不要浪费你的生命，在你一定会后悔的地方上。
逆水行舟，不进则退</description><language>zh-cn</language><lastBuildDate>Tue, 08 Jan 2013 11:04:10 GMT</lastBuildDate><pubDate>Tue, 08 Jan 2013 11:04:10 GMT</pubDate><ttl>60</ttl><item><title>（转载）海明距离</title><link>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197074.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Mon, 07 Jan 2013 08:37:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197074.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/197074.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197074.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/197074.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/197074.html</trackback:ping><description><![CDATA[<p style="box-sizing: border-box; font-size: 13px; color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">转自：<a href="http://blog.csdn.net/fuyangchang/article/details/5637464" style="box-sizing: border-box; color: #3468a4;">http://blog.csdn.net/fuyangchang/article/details/5637464</a><br style="box-sizing: border-box;" />wiki地址<a title="http://en.wikipedia.org/wiki/Hamming_distance" href="http://en.wikipedia.org/wiki/Hamming_distance" style="box-sizing: border-box; color: #336699; text-decoration: initial;">http://en.wikipedia.org/wiki/Hamming_distance</a></p><p style="box-sizing: border-box; font-size: 13px; color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">在信息领域，<strong style="box-sizing: border-box;">两个长度相等的字符串</strong>的海明距离是在相同位置上不同的字符的个数，也就是将一个字符串替换成另一个字符串需要的替换的次数。</p><p style="box-sizing: border-box; font-size: 13px; color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">例如：</p><ul style="list-style: none; margin: 0px 0px 10px 5px; padding-left: 5px; font-size: 11px; box-sizing: border-box; color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;"><li style="box-sizing: border-box;">"<strong style="box-sizing: border-box;">toned</strong>" and "<strong style="box-sizing: border-box;">roses</strong>" is 3.</li><li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">1011101</strong>&nbsp;and&nbsp;<strong style="box-sizing: border-box;">1001001</strong>&nbsp;is 2.</li><li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">2173896</strong>&nbsp;and&nbsp;<strong style="box-sizing: border-box;">2233796</strong>&nbsp;is 3.</li></ul><p style="box-sizing: border-box; font-size: 13px; color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">对于二进制来说，海明距离的结果相当于&nbsp;<em style="box-sizing: border-box;">a</em>&nbsp;<a href="http://en.wikipedia.org/wiki/Exclusive_OR" style="box-sizing: border-box; color: #336699; text-decoration: initial;">XOR</a>&nbsp;<em style="box-sizing: border-box;">b</em>&nbsp;结果中1的个数。</p><p style="box-sizing: border-box; font-size: 13px; color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;"></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">python<span style="box-sizing: border-box; font-family: 宋体;">代码如下</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">def hamming_distance(s1, s2):</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; assert len(s1) == len(s2)</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; return sum(ch1 != ch2 for ch1, ch2 in zip(s1, s2))</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">print (hamming_distance("gdad","glas"))</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-family: 宋体;">结果是</span>2</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">C<span style="box-sizing: border-box; font-family: 宋体;">语言代码如下</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">unsigned hamdist(unsigned x, unsigned y)</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">{</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp; unsigned dist = 0, val = x ^ y;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp; // Count the number of set bits</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp; while(val)</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp; {</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; ++dist;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; val &amp;= val - 1;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp; }</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp; return dist;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">}</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">int main()</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">{</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned x="abcdcc";</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned y="abccdd";</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned z=hamdist(x,y);</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%d",z);</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">}<br /><br />本文转自：http://www.cppblog.com/humanchao/archive/2012/12/26/196680.html</p><img src ="http://www.cppblog.com/wanghaiguang/aggbug/197074.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2013-01-07 16:37 <a href="http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197074.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转载）大小端问题</title><link>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197073.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Mon, 07 Jan 2013 08:33:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197073.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/197073.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197073.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/197073.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/197073.html</trackback:ping><description><![CDATA[<strong style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 16pt; font-family: 楷体_GB2312;">大小端问题</span></strong><strong style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"></strong><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">By unanao</span></strong></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">&lt;sunjianjiao@gmail.com&gt;</span></strong></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">一、什么是大小端问题</span></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">(From</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">《</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">Computer Systems,A Programer's Perspective</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">》</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">)</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">在几乎所有的机器上，多字节对象被存储为连续的字节序列，对象的地址为所使用字节序列中最低字节地址。</span></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; background-color: #ffffff; text-indent: 24pt; line-height: 18pt;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">小端：某些机器选择在存储器中按照从最低有效字节到最高有效字节的顺序存储对象，这种最低有效字节在最前面的表示方式被称为</span><strong style="box-sizing: border-box;"><em style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">小端法</span></em></strong><strong style="box-sizing: border-box;"><em style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">(little endian)</span></em></strong>&nbsp;<span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">。</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">这样的存储模式有点儿类似于把数据当作字符串顺序处理：地址由小向大增加，而数据从高位往低位放；</span></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; background-color: #ffffff; line-height: 18pt;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">大端：某些机器则按照从最高有效字节到最低有效字节的顺序储存，这种最高有效字节在最前面的方式被称为</span><strong style="box-sizing: border-box;"><em style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">大端法</span></em></strong><strong style="box-sizing: border-box;"><em style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">(big endian)</span></em></strong>&nbsp;<span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">。</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">这种存储模式将地址的高低和数据位权有效地结合起来，高地址部分权值高，低地址部分权值低，和我们的逻辑方法一致。</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;<span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">举个例子来说名大小端</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">:&nbsp;&nbsp;</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">比如一个</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">int x,&nbsp;</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">地址为</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">0x100,&nbsp;</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">它的值为</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">0x1234567.&nbsp;</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">则它所占据的</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">0x100, 0x101, 0x102, 0x103</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">地址组织如下图</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">:</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><br style="box-sizing: border-box;" /><img src="http://www.cppblog.com/images/cppblog_com/humanchao/%E6%96%B0%E5%BB%BA%E4%BD%8D%E5%9B%BE%E5%9B%BE%E5%83%8F.jpg" width="601" height="180" alt="" style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; background-color: #ffffff; line-height: 18pt;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">二、为什么会有大小端模式之分呢？</span></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; background-color: #ffffff; text-indent: 25.2pt; line-height: 18pt;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">这是因为在计算机系统中，我们是以字节为单位的，每个地址单元都对应着一个字节，一个字节为</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">&nbsp;8bit</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">。但是在</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">C</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">语言中除了</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">8bit</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">的</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">char</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">之外，还有</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">16bit</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">的</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">short</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">型，</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">32bit</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">的</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">long</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">型（要看具体的编译器），另外，对于位数大于</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">&nbsp;8</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">位的处理器，例如</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">16</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">位或者</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">32</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">位的处理器，由于寄存器宽度大于一个字节，那么必然存在着一个如果将多个字节安排的问题。因此就导致了大端存储模式和小端存储模式。例如一个</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">16bit</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">的</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">short</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">型</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">x</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">，在内存中的地址为</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">0x0010</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">，</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">x</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">的值为</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">0x1122</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">，那么</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">0x11</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">为高字节，</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">0x22</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">为低字节。对于</span>&nbsp;<span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">大端模式，就将</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">0x11</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">放在低地址中，即</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">0x0010</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">中，</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">0x22</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">放在高地址中，即</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">0x0011</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">中。小端模式，刚好相反。我们常用的</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">X86</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">结构是小端模</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">式，而</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">KEIL C51</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">则为大端模式。很多的</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">ARM</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">，</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">DSP</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">都为小端模式。有些</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif; letter-spacing: 0.4pt;">ARM</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312; letter-spacing: 0.4pt;">处理器还可以由硬件来选择是大端模式还是小端模式。</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">三、如何区分大小端问题：</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">方法</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">1</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">：</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><div style="background-color: #eeeeee; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #000000; ">#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">stdio.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;main(</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">)<br /></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #000000; ">{<br /></span><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pointer;<br /></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">&nbsp;8</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pointer&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;(unsigned&nbsp;</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">i;<br /></span><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pointer)<br /></span><span style="color: #008080; ">10</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">11</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">litttle_endian</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; ">12</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">13</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">14</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">15</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">big&nbsp;endian\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">17</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">18</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">19</span>&nbsp;<span style="color: #000000; ">}</span></div></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">C</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">中的数据类型都是从内存的低地址向高地址扩展，取址运算</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">"&amp;"</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">都是取低地址</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">。小端方式中（</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">i</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">占至少两个字节的长度）则</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">i</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">所分配的内存最小地址那个字节中就存着</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">1</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">，其他字节是</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">0</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">。</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">大端的话则</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">1</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">在</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">i</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">的最高地址字节处存放，</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">char</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">是一个字节，所以强制将</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">char</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">型量</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">p</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">指向</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">i</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">，</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">则</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">p</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">指向的一定是</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">i</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">的最低地址，那么就可以判断</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">p</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">中的值是不是</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">1</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">来确定是不是小端。</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">方法</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">2</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">：</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><div style="background-color: #eeeeee; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #000000; ">#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">stdio.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;main(</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">)<br /></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #000000; ">{<br /></span><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;union&nbsp;{<br /></span><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">short</span><span style="color: #000000; ">&nbsp;a;<br /></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;ch;<br /></span><span style="color: #008080; ">&nbsp;8</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;u;<br /></span><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u.a&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">10</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">11</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;(u.ch&nbsp;</span><span style="color: #000000; ">==</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">)<br /></span><span style="color: #008080; ">12</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">13</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">Littel&nbsp;endian\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; ">14</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">15</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">17</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">Big&nbsp;endian\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; ">18</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">19</span>&nbsp;<span style="color: #000000; ">}</span></div></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">利用联合体的特点，数据成员共享内存空间，</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">union</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">中元素的起始地址都是相同的</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">&#8212;&#8212;</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">位于联合的开始。</span>&nbsp;<span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">用</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">char</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">来截取感兴趣的字节<span style="box-sizing: border-box; color: #2b2bd5;">。</span></span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">四、需要考虑大小端（字节顺序）的情况</span></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">1</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">、所写的程序需要向不同的硬件平台迁移，说不定哪一个平台是大端还是小端，为了保证可移植性，一定提前考虑好。</span></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">2.&nbsp;</span></strong><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">在不同类型的机器之间通过网络传送二进制数据时。</span>&nbsp;<span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">一个常见的问题是当小端法机器产生的数据被发送到大端法机器或者反之时，接受程序会发现，字</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">(word)</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">里的字节</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">(byte)</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">成了反序的。为了避免这类问</span>&nbsp;<span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">题，网络应用程序的代码编写必须遵守已建立的关于字节顺序的规则，以确保发送方机器将它的内部表示转换成网络标准，而接受方机器则将网络标准转换为它的内部标准。</span></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">3.&nbsp;</span></strong><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">当阅读表示整数的字节序列时。这通常发生在检查机器级程序时，</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">e.g.</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">：反汇编得到的一条指令：</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;"><br style="box-sizing: border-box;" />80483bd: 01 05 64 94 04 08&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add %eax, 0x8049464</span></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">3.&nbsp;</span></strong><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">当编写强转的类型系统的程序时。</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">如写入的数据为</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">u32</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">型，但是读取的时候却是</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">char</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">型的。如：</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">0x1234,&nbsp;</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">大端读取为</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">12</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">时，小端独到的是</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">34</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">。</span></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">六、提高程序的可移植性</span></strong><strong style="box-sizing: border-box;"></strong></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">使用宏编译</span></strong><strong style="box-sizing: border-box;"></strong></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">#ifdef LITTLE_ENDIAN</span></strong></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">//</span></strong><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">小端的代码</span></strong><strong style="box-sizing: border-box;"></strong></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">#else</span></strong></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">//</span></strong><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">大端的代码</span></strong><strong style="box-sizing: border-box;"></strong></p><p align="left" style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">#endif</span></strong></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;">&nbsp;</p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">七、大、小端之间的转换</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;">1</span><span style="box-sizing: border-box; font-size: 12pt; font-family: 楷体_GB2312;">、小端转换为大端</span></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><div style="background-color: #eeeeee; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #000000; ">#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">stdio.h</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /></span><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;show_byte(</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">addr,&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;len)<br /></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #000000; ">{<br /></span><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i;<br /></span><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">&nbsp;(i&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;&nbsp;i&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;len;&nbsp;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br /></span><span style="color: #008080; ">&nbsp;8</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%.2x&nbsp;\t</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,&nbsp;addr[i]);<br /></span><span style="color: #008080; ">10</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">11</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; ">12</span>&nbsp;<span style="color: #000000; ">}<br /></span><span style="color: #008080; ">13</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">14</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;endian_convert(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;t)<br /></span><span style="color: #008080; ">15</span>&nbsp;<span style="color: #000000; ">{<br /></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;result;<br /></span><span style="color: #008080; ">17</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i;<br /></span><span style="color: #008080; ">18</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">19</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">20</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">&nbsp;(i&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;&nbsp;i&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(t);&nbsp;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br /></span><span style="color: #008080; ">21</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">22</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</span><span style="color: #000000; ">&lt;&lt;=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">8</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">23</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;</span><span style="color: #000000; ">|=</span><span style="color: #000000; ">&nbsp;(t&nbsp;</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0xFF</span><span style="color: #000000; ">);<br /></span><span style="color: #008080; ">24</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;</span><span style="color: #000000; ">&gt;&gt;=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">8</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">25</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">26</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">27</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;result;<br /></span><span style="color: #008080; ">28</span>&nbsp;<span style="color: #000000; ">}<br /></span><span style="color: #008080; ">29</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">30</span>&nbsp;<span style="color: #000000; "></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;main(</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">)<br /></span><span style="color: #008080; ">31</span>&nbsp;<span style="color: #000000; ">{<br /></span><span style="color: #008080; ">32</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i;<br /></span><span style="color: #008080; ">33</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;ret;<br /></span><span style="color: #008080; ">34</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">35</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0x1234567</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">36</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">37</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;show_byte((</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">i,&nbsp;</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">));<br /></span><span style="color: #008080; ">38</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;endian_convert(i);<br /></span><span style="color: #008080; ">39</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;show_byte((</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">ret,&nbsp;</span><span style="color: #0000FF; ">sizeof</span><span style="color: #000000; ">(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">));<br /></span><span style="color: #008080; ">40</span>&nbsp;<span style="color: #000000; ">&nbsp;<br /></span><span style="color: #008080; ">41</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">42</span>&nbsp;<span style="color: #000000; ">}</span></div></p><p style="box-sizing: border-box; color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20px; background-color: #ffffff;"><span style="box-sizing: border-box; font-size: 12pt; font-family: 'Times New Roman', serif;"><br />本文转自：</span>http://www.cppblog.com/humanchao/archive/2012/12/26/196684.html</p><img src ="http://www.cppblog.com/wanghaiguang/aggbug/197073.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2013-01-07 16:33 <a href="http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197073.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转载）循环有序数组查找问题</title><link>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197072.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Mon, 07 Jan 2013 08:31:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197072.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/197072.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197072.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/197072.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/197072.html</trackback:ping><description><![CDATA[<p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px;"><strong style="box-sizing: border-box;">题目描述：</strong></span></span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;一个循环有序数组（如：3,4,5,6,7,8,9,0,1,2），不知道其最小值的位置，要查找任一数值的位置。要求算法时间复杂度为log2(n)。</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><br style="box-sizing: border-box;" /></span><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;"><strong style="box-sizing: border-box;">问题分析：</strong></span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;我们可以把循环有序数组分为左右两部分（以mid = （low+high）/ 2为界），由循环有序数组的特点知，左右两部分必有一部分是有序的，我们可以找出有序的这部分，然后看所查找元素是否在有序部分，若在，则直接对有序部分二分查找，若不在，对无序部分递归调用查找函数。</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;"><strong style="box-sizing: border-box;">代码如下：</strong></span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;#include &lt;iostream&gt;</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;using namespace std;</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;int binarySearch(int a[],int low,int high,int value)&nbsp;<wbr style="box-sizing: border-box;">&nbsp;//二分查找<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;{<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;if(low&gt;high)<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;return -1;</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;int mid=(low+high)/2;</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;if(value==a[mid])<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;return mid;<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;else if(value&gt;a[mid])<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;return binarySearch(a,mid+1,high,value);<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;else<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;return binarySearch(a,low,mid-1,value);<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;}</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;int Search(int a[],int low,int high,int value)&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;//循环有序查找函数<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;{<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;int mid=(low+high)/2;</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;if(a[mid]&gt;a[low])&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;//左有序<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;{<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;if(a[low]&lt;=value &amp;&amp; value&lt;=a[mid] )&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;//说明value在左边，直接二分查找<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;{<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;return binarySearch(a,low,mid,value);<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;}</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;else&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;//value在右边<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;{<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;return Search(a,mid+1,high,value);<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;}<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;}<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;else&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;//右有序<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;{<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;if(a[mid]&lt;=value &amp;&amp; value&lt;=a[high])<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;{<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;return binarySearch(a,mid,high,value);<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;}<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;else<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;{<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;return Search(a,low,mid-1,value);<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;}<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;}<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;}</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;int main()<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;{<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;int a[]={3,4,5,6,7,8,9,0,1,2};</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;cout&lt;&lt;Search(a,0,9,0)&lt;&lt;endl;</span></span></p><p style="box-sizing: border-box; font-size: 13px; line-height: 20px; margin: 1em 0px 0.5em; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;return 0;<br style="box-sizing: border-box;" />&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;<wbr style="box-sizing: border-box;">&nbsp;}</span></span></p><div><span style="box-sizing: border-box; word-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; word-wrap: normal; word-break: normal; line-height: 19px; font-size: small;"><br />本文转自：</span></span>http://www.cppblog.com/humanchao/archive/2012/12/26/196686.html</div><img src ="http://www.cppblog.com/wanghaiguang/aggbug/197072.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2013-01-07 16:31 <a href="http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197072.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转载）经典的String Hash算法</title><link>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197071.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Mon, 07 Jan 2013 08:29:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197071.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/197071.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197071.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/197071.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/197071.html</trackback:ping><description><![CDATA[<span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">设计高效算法往往需要使用Hash表，O(1)级的查找速度是任何别的算法无法比拟的。</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">所谓Hash，一般是一个整数，通过某种算法，可以把一个字符串"pack"成一个整数，这个数称为Hash，当然，一个整数是无法对应一个字符串的。</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">所以Hash函数是Hash表最核心的部分，对于一个Hash函数，评价其优劣的标准应为随机性或离散性，即对任意一组标本，进入Hash表每一个单元（cell）之概率的平均程度，因为这个概率越平均，两个字符串计算出的Hash值相等hash collision的可能越小，数据在表中的分布就越平均，表的空间利用率就越高。</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">Hash表的构造和冲突的不同实现方法对执行效率也有一定的影响.</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">DJBHash是一种非常流行的算法，俗称"Times33"算法。Times33的算法很简单，就是不断的乘33，原型如下</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">hash(i) = hash(i-1) * 33 + str[i]</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">Time33在效率和随机性两方面上俱佳。</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">其它常用字符串哈希函数有：</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">BKDRHash，APHash，JSHash，RSHash，SDBMHash，PJWHash，ELFHash等。BKDRHash和APHash也是比较优秀的算法。当然要根据具体应用选择合适的Hash算法，比如字符集的考虑。</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">APHash作者Arash Partow有一个页面很有参考价值，包括了各种Hash的介绍及代码。</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">http://www.partow.net/programming/hashfunctions/#RSHashFunction</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">Blizzard使用的算法比较精妙，被称为"One-Way Hash"，并且在Hash表中使用了三个哈希值(一个用来确定位置，另外两个用来校验)。</span><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><br style="box-sizing: border-box; font-size: 13px; line-height: 25px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #f6f6ed;" /><span style="box-sizing: border-box; font-size: 13px; color: #6e6e6e; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 25px; background-color: #f6f6ed;">MD5等加密算法也属于hash，不过已被中国学者找到碰撞检测的破解算法<br /></span><br />本文转自：http://www.cppblog.com/humanchao/archive/2012/12/26/196690.html<img src ="http://www.cppblog.com/wanghaiguang/aggbug/197071.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2013-01-07 16:29 <a href="http://www.cppblog.com/wanghaiguang/archive/2013/01/07/197071.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>散列表（哈希表）</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/11/21/195463.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Wed, 21 Nov 2012 06:22:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/11/21/195463.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/195463.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/11/21/195463.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/195463.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/195463.html</trackback:ping><description><![CDATA[<a target="_blank" href="http://baike.baidu.com/view/1320746.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 25px; background-color: #ffffff;"><span style="font-family: Verdana;">散列表</span></a><span style="font-family: Verdana; font-size: 14px; line-height: 25px; background-color: #ffffff;">（Hash table，也叫哈希表），是根据关键码值(Key value)而直接进行访问的</span><a target="_blank" href="http://baike.baidu.com/view/9900.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 25px; background-color: #ffffff;"><span style="font-family: Verdana;">数据结构</span></a><span style="font-family: Verdana; font-size: 14px; line-height: 25px; background-color: #ffffff;">。也就是说，它通过把关键码值映射到表中一个位置来访问记录，以加快查找的速度。这个映射函数叫做</span><a target="_blank" href="http://baike.baidu.com/view/131153.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 25px; background-color: #ffffff;"><span style="font-family: Verdana;">散列函数</span></a><span style="font-family: Verdana; font-size: 14px; line-height: 25px; background-color: #ffffff;">，存放记录的</span><a target="_blank" href="http://baike.baidu.com/view/209670.htm" style="color: #136ec2; font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 25px; background-color: #ffffff;"><span style="font-family: Verdana;">数组</span></a><span style="font-family: Verdana; font-size: 14px; line-height: 25px; background-color: #ffffff;">叫做散列表。<br /></span><p style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><strong><span style="line-height: 1.5; font-size: 12pt; font-family: Verdana;">哈希表算法-哈希表的构造方法</span></strong></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">１、直接定址法</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">例如：有一个从1到100岁的人口数字统计表，其中，年龄作为关键字，哈希函数取关键字自身。</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 但这种方法效率不高,时间复杂度是O(1),空间复杂度是O(n),n是关键字的个数</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;">&nbsp;</p><div class="img img_r" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff;"><a target="_blank" href="http://tupian.hudong.com/a2_17_42_01300000185725121508427196756_jpg.html" title="点击查看原图" style="color: navy; text-decoration: initial;"><img src="http://a2.att.hudong.com/17/42/01300000185725121508427196756_s.jpg" alt="哈希表算法" title="哈希表算法" style="border: 0px;" /></a><strong style="font-family: Verdana;">哈希表算法</strong></div><p style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;">&nbsp;</p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">２、数字分析法</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">有学生的生日数据如下：</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">年.月.日</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">75.10.03</span><br /><span style="font-family: Verdana;">75.11.23</span><br /><span style="font-family: Verdana;">76.03.02</span><br /><span style="font-family: Verdana;">76.07.12</span><br /><span style="font-family: Verdana;">75.04.21</span><br /><span style="font-family: Verdana;">76.02.15</span><br /><span style="font-family: Verdana;">...</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">经分析,第一位，第二位，第三位重复的可能性大，取这三位造成冲突的机会增加，所以尽量不取前三位，取后三位比较好。</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">３、平方取中法</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">取关键字平方后的中间几位为</span><a href="" title="哈希地址" class="link_red" style="color: navy; text-decoration: initial;"><span style="font-family: Verdana;">哈希地址</span></a><span style="font-family: Verdana;">。</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">４、折叠法</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">将关键字分割成位数相同的几部分（最后一部分的位数可以不同），然后取这几部分的叠加和（舍去进位）作为哈希地址，这方法称为折叠法。</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">例如：每一种西文图书都有一个国际标准图书编号，它是一个10位的十进制数字，若要以它作关键字建立一个哈希表，当馆藏书种类不到10,000时，可采用此法构造一个四位数的哈希函数。如果一本书的编号为0-442-20586-4,则：</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;">&nbsp;</p><div class="img img_r" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff;"><a target="_blank" href="http://tupian.hudong.com/a0_04_43_01300000185725121508435076717_jpg.html" title="点击查看原图" style="color: navy; text-decoration: initial;"><img src="http://a0.att.hudong.com/04/43/01300000185725121508435076717_s.jpg" alt="哈希表算法" title="哈希表算法" style="border: 0px;" /></a><strong style="font-family: Verdana;">哈希表算法</strong></div><p style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;">&nbsp;</p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">５、除留余数法</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">取关键字被某个不大于哈希表表长m的数p除后所得余数为哈希地址。</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">H(key)=key MOD p (p&lt;=m)</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">６、随机数法</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">选择一个随机函数，取关键字的随机函数值为它的哈希地址，即</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">H(key)=random(key) ,其中random为随机函数。通常用于关键字长度不等时采用此法。</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">５、除留余数法</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">取关键字被某个不大于哈希表表长m的数p除后所得余数为哈希地址。</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">H(key)=key MOD p (p&lt;=m)</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">６、随机数法</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">选择一个随机函数，取关键字的随机函数值为它的哈希地址，即</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">H(key)=random(key) ,其中random为随机函数。通常用于关键字长度不等时采用此法。</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">５、除留余数法</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">取关键字被某个不大于哈希表表长m的数p除后所得余数为哈希地址。</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">H(key)=key MOD p (p&lt;=m)</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">６、随机数法</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">选择一个</span><a href="" title="随机函数" class="link_red" style="color: navy; text-decoration: initial;"><span style="font-family: Verdana;">随机函数</span></a><span style="font-family: Verdana;">，取关键字的随机函数值为它的哈希地址，即</span></p><p align="left" style="font-size: 13px; line-height: 1.4; margin-top: 10px; margin-bottom: 10px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: #ffffff;"><span style="font-family: Verdana;">H(key)=random(key) ,其中random为随机函数。通常用于关键字长度不等时采用此法。</span></p><span style="font-family: Verdana; font-size: 14px; line-height: 25px; background-color: #ffffff;"><br /></span><span style="color: #333333; font-family: Verdana; font-size: 12pt; line-height: 24px; background-color: #ffffff;"><strong>处理冲突的方法</strong></span><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">&nbsp;</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　通常有两类方法处理冲突：开放定址(Open&nbsp;Addressing)法和拉链(Chaining)法。前者是将所有结点均存放在散列表T[0..m-1]中；后者通常是将互为同义词的结点链成一个单链表，而将此链表的头指针放在散列表T[0..m-1]中。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">1、开放定址法</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">（1）开放地址法解决冲突的方法</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　用开放定址法解决冲突的做法是：当冲突发生时，使用某种探查(亦称探测)技术在散列表中形成一个探查(测)序列。沿此序列逐个单元地查找，直到找到给定&nbsp;的关键字，或者碰到一个开放的地址(即该地址单元为空)为止（若要插入，在探查到开放的地址，则可将待插入的新结点存人该地址单元）。查找时探查到开放的&nbsp;地址则表明表中无待查的关键字，即查找失败。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">注意：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">&#9312;用开放定址法建立散列表时，建表前须将表中所有单元(更严格地说，是指单元中存储的关键字)置空。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">&#9313;空单元的表示与具体的应用相关。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">【例】关键字均为非负数时，可用"-1"来表示空单元，而关键字为字符串时，空单元应是空串。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">总之：应该用一个不会出现的关键字来表示空单元。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">（2）开放地址法的一般形式</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">开放定址法的一般形式为：&nbsp;hi=(h(key)+di)％m&nbsp;1&#8804;i&#8804;m-1&nbsp;</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">其中：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　&nbsp;&#9312;h(key)为散列函数，di为增量序列，m为表长。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　&#9313;h(key)是初始的探查位置，后续的探查位置依次是hl，h2，&#8230;，hm-1，即h(key)，hl，h2，&#8230;，hm-1形成了一个探查序列。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　&nbsp;&#9314;若令开放地址一般形式的i从0开始，并令d0=0，则h0=h(key)，则有：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">hi=(h(key)+di)％m&nbsp;0&#8804;i&#8804;m-1&nbsp;</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">探查序列可简记为hi(0&#8804;i&#8804;m-1)。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">（3）开放地址法堆装填因子的要求</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　开放定址法要求散列表的装填因子&#945;&#8804;l，实用中取&#945;为0.5到0.9之间的某个值为宜。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">（4）形成探测序列的方法</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　按照形成探查序列的方法不同，可将开放定址法区分为线性探查法、二次探查法、双重散列法等。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">&#9312;线性探查法(Linear&nbsp;Probing)</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">该方法的基本思想是：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">将散列表T[0..m-1]看成是一个循环向量，若初始探查的地址为d(即h(key)=d)，则最长的探查序列为：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">d，d+l，d+2，&#8230;，m-1，0，1，&#8230;，d-1</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　即:探查时从地址d开始，首先探查T[d]，然后依次探查T[d+1]，&#8230;，直到T[m-1]，此后又循环到T[0]，T[1]，&#8230;，直到探查到T[d-1]为止。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">探查过程终止于三种情况：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　(1)若当前探查的单元为空，则表示查找失败（若是插入则将key写入其中）；</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">&nbsp; &nbsp;(2)若当前探查的单元中含有key，则查找成功，但对于插入意味着失败；</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　(3)若探查到T[d-1]时仍未发现空单元也未找到key，则无论是查找还是插入均意味着失败(此时表满)。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">利用开放地址法的一般形式，线性探查法的探查序列为：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">hi=(h(key)+i)％m&nbsp;0&#8804;i&#8804;m-1&nbsp;//即di=i</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">利用线性探测法构造散列表</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">【例9.1】已知一组关键字为(26，36，41，38，44，15，68，12，06，51)，用除余法构造散列函数，用线性探查法解决冲突构造这组关键字的散列表。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">解答:为了减少冲突，通常令装填因子&#945;</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　由除余法的散列函数计算出的上述关键字序列的散列地址为(0，10，2，12，5，2，3，12，6，12)。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　前5个关键字插入时，其相应的地址均为开放地址，故将它们直接插入T[0]，T[10)，T[2]，T[12]和T[5]中。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　当插入第6个关键字15时，其散列地址2(即h(15)=15％13=2)已被关键字41(15和41互为同义词)占用。故探查h1=(2+1)％13=3，此地址开放，所以将15放入T[3]中。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　当插入第7个关键字68时，其散列地址3已被非同义词15先占用，故将其插入到T[4]中。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　当插入第8个关键字12时，散列地址12已被同义词38占用，故探查hl=(12+1)％13=0，而T[0]亦被26占用，再探查h2=(12+2)％13=1，此地址开放，可将12插入其中。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　类似地，第9个关键字06直接插入T[6]中；而最后一个关键字51插人时，因探查的地址12，0，1，&#8230;，6均非空，故51插入T[7]中。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　构造散列表的具体过程【参见动画演示】</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">聚集或堆积现象</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">用线性探查法解决冲突时，当表中i,i+1，&#8230;，i+k的位置上已有结点时，一个散列地址为i，i+1，&#8230;，i+k+1的结点都将插入在位置i+k+1&nbsp;上。把这种散列地址不同的结点争夺同一个后继散列地址的现象称为聚集或堆积(Clustering)。这将造成不是同义词的结点也处在同一个探查序列之&nbsp;中，从而增加了探查序列的长度，即增加了查找时间。若散列函数不好或装填因子过大，都会使堆积现象加剧。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">【例】上例中，h(15)=2，h(68)=3，即15和68不是同义词。但由于处理15和同义词41的冲突时，15抢先占用了T[3]，这就使得插入68时，这两个本来不应该发生冲突的非同义词之间也会发生冲突。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　为了减少堆积的发生，不能像线性探查法那样探查一个顺序的地址序列(相当于顺序查找)，而应使探查序列跳跃式地散列在整个散列表中。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">&#9313;二次探查法(Quadratic&nbsp;Probing)</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　二次探查法的探查序列是：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">hi=(h(key)+i*i)％m&nbsp;0&#8804;i&#8804;m-1&nbsp;//即di=i2</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">即探查序列为d=h(key)，d+12，d+22，&#8230;，等。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　该方法的缺陷是不易探查到整个散列空间。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">&#9314;双重散列法(Double&nbsp;Hashing)</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　该方法是开放定址法中最好的方法之一，它的探查序列是：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">hi=(h(key)+i*h1(key))％m&nbsp;0&#8804;i&#8804;m-1&nbsp;//即di=i*h1(key)</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　即探查序列为：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">d=h(key)，(d+h1(key))％m，(d+2h1(key))％m，&#8230;，等。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　该方法使用了两个散列函数h(key)和h1(key)，故也称为双散列函数探查法。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">注意：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">定义h1(key)的方法较多，但无论采用什么方法定义，都必须使h1(key)的值和m互素，才能使发生冲突的同义词地址均匀地分布在整个表中，否则可能造成同义词地址的循环计算。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">【例】若m为素数，则h1(key)取1到m-1之间的任何数均与m互素，因此，我们可以简单地将它定义为：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">h1(key)=key％(m-2)+1</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">【例】对例9.1，我们可取h(key)=key％13，而h1(key)=key％11+1。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">【例】若m是2的方幂，则h1(key)可取1到m-1之间的任何奇数。</span><br /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">2、拉链法</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">（1）拉链法解决冲突的方法</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　拉链法解决冲突的做法是：将所有关键字为同义词的结点链接在同一个单链表中。若选定的散列表长度为m，则可将散列表定义为一个由m个头指针组成的指针数&nbsp;组T[0..m-1]。凡是散列地址为i的结点，均插入到以T[i]为头指针的单链表中。T中各分量的初值均应为空指针。在拉链法中，装填因子&#945;可以大于&nbsp;1，但一般均取&#945;&#8804;1。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">【例9.2】已知一组关键字和选定的散列函数和例9.1相同，用拉链法解决冲突构造这组关键字的散列表。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">解答：不妨和例9.1类似，取表长为13，故散列函数为h(key)=key％13，散列表为T[0..12]。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">注意：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">当把h(key)=i的关键字插入第i个单链表时，既可插入在链表的头上，也可以插在链表的尾上。这是因为必须确定key不在第i个链表时，才能将它插入&nbsp;表中，所以也就知道链尾结点的地址。若采用将新关键字插入链尾的方式，依次把给定的这组关键字插入表中，则所得到的散列表如下图所示。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　具体构造过程【参见动画演示】。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><img src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/hash.jpg" width="419" height="368" alt="" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">（2）拉链法的优点</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　与开放定址法相比，拉链法有如下几个优点：</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">(1)拉链法处理冲突简单，且无堆积现象，即非同义词决不会发生冲突，因此平均查找长度较短；</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">(2)由于拉链法中各链表上的结点空间是动态申请的，故它更适合于造表前无法确定表长的情况；</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">(3)开放定址法为减少冲突，要求装填因子&#945;较小，故当结点规模较大时会浪费很多空间。而拉链法中可取&#945;&#8805;1，且结点较大时，拉链法中增加的指针域可忽略不计，因此节省空间；</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">(4)在用拉链法构造的散列表中，删除结点的操作易于实现。只要简单地删去链表上相应的结点即可。而对开放地址法构造的散列表，删除结点不能简单地将&nbsp;被删结点的空间置为空，否则将截断在它之后填人散列表的同义词结点的查找路径。这是因为各种开放地址法中，空地址单元(即开放地址)都是查找失败的条件。&nbsp;因此在用开放地址法处理冲突的散列表上执行删除操作，只能在被删结点上做删除标记，而不能真正删除结点。</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">（3）拉链法的缺点</span><br style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;" /><span style="color: #333333; font-family: Verdana; font-size: 14px; line-height: 24px; background-color: #ffffff;">　拉链法的缺点是：指针需要额外的空间，故当结点规模较小时，开放定址法较为节省空间，而若将节省的指针空间用来扩大散列表的规模，可使装填因子变小，这又减少了开放定址法中的冲突，从而提高平均查找速度。</span><br style="font-family: Verdana;" /><br />本文转自：http://bbs.csdn.net/topics/320198804<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://www.cnblogs.com/jiewei915/archive/2010/08/09/1796042.html<img src ="http://www.cppblog.com/wanghaiguang/aggbug/195463.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-11-21 14:22 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/11/21/195463.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>算法导论——基数排序（网易公开课）</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/11/13/195119.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Tue, 13 Nov 2012 08:52:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/11/13/195119.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/195119.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/11/13/195119.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/195119.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/195119.html</trackback:ping><description><![CDATA[<p><strong style="font-family: Verdana; font-size: 12pt">基数排序</strong><span style="font-size: 12pt">(Radix sort)是一种非比较型</span><a title="整数" href="http://zh.wikipedia.org/wiki/%E6%95%B4%E6%95%B0"><span style="font-size: 12pt">整数</span></a><a title="排序算法" href="http://zh.wikipedia.org/wiki/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95"><span style="font-size: 12pt">排序算法</span></a><span style="font-size: 12pt">，</span><span style="font-size: 12pt">其原理是将整数按位数切割成不同的数字，然后按每个位数分别比较。由于整数也可以表达字符串（比如名字或日期）和特定格式的浮点数，所以基数排序也不是只能使用于整数</span><span style="font-family: Verdana; font-size: 12pt">。基数</span><span style="font-family: Verdana; font-size: 12pt">排序的发明可以追溯</span><span style="font-family: Verdana; font-size: 12pt">到1887年</span><a title="赫尔曼&#183;何乐礼" href="http://zh.wikipedia.org/wiki/%E8%B5%AB%E7%88%BE%E6%9B%BC%C2%B7%E4%BD%95%E6%A8%82%E7%A6%AE"><span style="font-family: Verdana; font-size: 12pt">赫尔曼&#183;何乐礼</span></a><span style="font-size: 12pt">在</span><a class="new" title="打孔卡片制表机（页面不存在）" href="http://zh.wikipedia.org/w/index.php?title=%E6%89%93%E5%AD%94%E5%8D%A1%E7%89%87%E5%88%B6%E8%A1%A8%E6%9C%BA&amp;action=edit&amp;redlink=1"><span style="font-size: 12pt">打孔卡片制表机</span></a><span style="font-size: 12pt">(Tabulation Machine)上的贡献</span><span style="font-size: 12pt">。</span></p>
<p><span style="font-family: Verdana; font-size: 12pt">它是这样实现的：将</span><span style="font-family: Verdana; font-size: 12pt">所有待比较数值（正整数）统一为同样的数位长度，数位较短的数前面补零。然后，从最低位开始，依次进行一次排序。这样从最低位排序一直到最高位排序完成以后, 数列就变成一个有序序列。</span></p>
<p><span style="font-family: Verdana; font-size: 12pt">基数排序时对每一维进行调用子排序算法时要求这</span><span style="font-family: Verdana; font-size: 12pt">个子排序算法必须是稳定的。</span><br /><span style="font-family: Verdana; font-size: 12pt">基数排序与直觉相反：它是按照从底位到高位的顺序排序的。</span><br /></p>
<p style="text-indent: 2em"><strong style="font-family: Verdana; font-size: 12pt">伪代码：</strong></p>
<p style="text-indent: 2em"><span style="font-family: Verdana; font-size: 12pt">RADIX-SORT(A,d)</span></p>
<p style="text-indent: 2em"><span style="font-family: Verdana; font-size: 12pt">1&nbsp;&nbsp;&nbsp; for i &lt;-- 1 to d</span></p>
<p style="text-indent: 2em"><span style="font-family: Verdana; font-size: 12pt">2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do use a stable sort to sort array A on digit i</span></p>
<p>&nbsp;</p>
<p style="text-indent: 2em"><strong style="font-family: Verdana; font-size: 12pt">引理8.3：</strong><span style="font-size: 12pt"> 给定n个d位数，每一个数位可以取k种可能的值，如果所用稳定排序需要&#920;(n+k)时间，基数排序能以&#920;(d(n+k))的时间完成。</span></p>
<p style="text-indent: 2em"><span style="font-family: Verdana; font-size: 12pt">证明：如果采用计数排序这种稳定排序，那么每一遍处理需要时间&#920;(n+k)，一共需处理d遍，于是基数排序的运行时间为&#920;(d(n+k))。当d为常数，k=O(n)时，有线性运行时间。</span></p>
<p style="text-indent: 2em"><em style="font-family: Verdana; font-size: 12pt"><strong>注：</strong></em><span style="font-size: 12pt">将关键字分成若干位方面，可以有一定的灵活性。</span></p>
<p style="text-indent: 2em"><strong style="font-family: Verdana; font-size: 12pt">引理8.4</strong>：<span style="font-size: 12pt">给定n个b位数和任何正整数r&lt;=b，如果采用的稳定排序需要&#920;(d(n+k))时间，则RADIX-SORT能在&#920;((b/r)(n+2r))时间内正确地对这些数进行排序。</span></p>
<p style="text-indent: 2em"><span style="font-family: Verdana; font-size: 12pt">证明：对于一个r&lt;=b,将每个关键字看成由d=b/r位组成的数，每</span><span style="font-family: Verdana; font-size: 12pt">一个数字都是(0~ -1)之间的一个整数，这样就可以采取计数排序。K= ，d=b/r，总的运行时间为&#920;((b/r)(n+ ))。</span></p>
<p style="text-indent: 2em"><span style="font-family: Verdana; font-size: 12pt">对于给定的n值和b值，</span><span style="font-family: Verdana; font-size: 12pt">如何选择r值使得最小化表达式(b/r)(n+2r)。如果b&lt; lgn</span><span style="font-family: Verdana; font-size: 12pt">，对于任何r&lt;=b的值，都有(n+2r)=&#920;(n)，于是选择r=b，使计数排序的时间为&#920;((b/b)(n+2b)) = &#920;(n)。 如果b&gt;lgn</span><span style="font-family: Verdana; font-size: 12pt">，则选择r=lgn，可以给出在某一常数因子内的最佳时间：当r=lgn时，算法复杂度为&#920;(bn/lgn)，当r增大到lgn以上时，分子 增大比分母r快，于是运行时间复杂度为&#937;(bn/lgn)；反之当r减小到lgn以下的时候，b/r增大，而n+ 仍然是&#920;(n)。</span><br /><br /><span style="font-family: Verdana; font-size: 12pt">以下引自麻省理工学院算法导论&#8212;&#8212;笔记：</span><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/RadixSort1.jpg" longdesc="" /><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/RadixSort2.jpg" width="583" longdesc="" height="408" /><br /><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/RadixSort3.jpg" width="589" longdesc="" height="412" /><br /><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/RadixSort4.jpg" width="574" longdesc="" height="411" /><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/RadixSort5.jpg" width="591" longdesc="" height="410" /><br /><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/RadixSort6.jpg" width="557" longdesc="" height="413" /><br /><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/RadixSort7.jpg" longdesc="" /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/RadixSort8.jpg" width="579" longdesc="" height="401" /><br /><br />以下代码实例转自：<a href="http://www.docin.com/p-449224125.html">http://www.docin.com/p-449224125.html</a><br /></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #000000">以下是用C</span><span style="color: #000000">++</span><span style="color: #000000">实现的基数排序代码：<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;这个是基数排序用到的计数排序，是稳定的。<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #008000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;pDigit是基数数组,nMax是基数的上限，pData是待排序的数组，&nbsp;nLen是待排序数组的元素个数<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #008000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;必须pDigit和pData的下标相对应的，即pDigit[1]对应pData[1]</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;RadixCountingSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pDigit,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nMax,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pData,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nLen){<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;以下是计数排序</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pCount&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">[nMax];<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pSorted&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">[nLen];<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j;<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">nMax;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">pCount[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">nLen;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000"></span><span style="color: #000000">++</span><span style="color: #000000">pCount[pDigit[i]];<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">nMax;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">pCount[i]&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;pCount[i</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">nLen</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">i){<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000"></span><span style="color: #000000">--</span><span style="color: #000000">pCount[pDigit[i]];<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">pSorted[pCount[pDigit[i]]]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pData[i];&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;z这里注意，是把待排序的数组赋值</span><span style="color: #008000"><br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">}<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">nLen;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">pData[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pSorted[i];<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">delete&nbsp;[]&nbsp;pCount;<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">delete&nbsp;[]&nbsp;pSorted;<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;RadixSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pData,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nLen){<br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pDigit&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">[nLen];&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;申请存放基数(某个位数)的空间</span><span style="color: #008000"><br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nRadixBase&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;flag&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">while</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">flag){<br /></span><span style="color: #008080">33</span>&nbsp;<span style="color: #000000">flag&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /></span><span style="color: #008080">34</span>&nbsp;<span style="color: #000000">nRadixBase&nbsp;</span><span style="color: #000000">*=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">10</span><span style="color: #000000">;<br /></span><span style="color: #008080">35</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">nLen;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i){<br /></span><span style="color: #008080">36</span>&nbsp;<span style="color: #000000">pDigit[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pData[i]</span><span style="color: #000000">%</span><span style="color: #000000">nRadixBase;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;求出某位上的数当做基数</span><span style="color: #008000"><br /></span><span style="color: #008080">37</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">pDigit[i]&nbsp;</span><span style="color: #000000">/=</span><span style="color: #000000">&nbsp;nRadixBase</span><span style="color: #000000">/</span><span style="color: #000000">10</span><span style="color: #000000">;<br /></span><span style="color: #008080">38</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">if</span><span style="color: #000000">(pDigit[i]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)<br /></span><span style="color: #008080">39</span>&nbsp;<span style="color: #000000">flag&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /></span><span style="color: #008080">40</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">41</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">if</span><span style="color: #000000">(flag)<br /></span><span style="color: #008080">42</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">43</span>&nbsp;<span style="color: #000000">RadixCountingSort(pDigit,</span><span style="color: #000000">10</span><span style="color: #000000">,pData,nLen);<br /></span><span style="color: #008080">44</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">45</span>&nbsp;<span style="color: #000000">delete[]&nbsp;pDigit;<br /></span><span style="color: #008080">46</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">47</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">48</span>&nbsp;<span style="color: #000000">main()<br /></span><span style="color: #008080">49</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">50</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nData[</span><span style="color: #000000">10</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">{</span><span style="color: #000000">43</span><span style="color: #000000">,</span><span style="color: #000000">65</span><span style="color: #000000">,</span><span style="color: #000000">34</span><span style="color: #000000">,</span><span style="color: #000000">5</span><span style="color: #000000">,</span><span style="color: #000000">8</span><span style="color: #000000">,</span><span style="color: #000000">34</span><span style="color: #000000">,</span><span style="color: #000000">23</span><span style="color: #000000">,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #000000">45</span><span style="color: #000000">,</span><span style="color: #000000">34</span><span style="color: #000000">};;<br /></span><span style="color: #008080">51</span>&nbsp;<span style="color: #000000">RadixSort(nData,&nbsp;</span><span style="color: #000000">10</span><span style="color: #000000">);<br /></span><span style="color: #008080">52</span>&nbsp;<span style="color: #000000">printf(</span><span style="color: #000000">"</span><span style="color: #000000">经排序后的数列是：\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">53</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">10</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">54</span>&nbsp;<span style="color: #000000">printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;nData[i]);<br /></span><span style="color: #008080">55</span>&nbsp;<span style="color: #000000">printf(</span><span style="color: #000000">"</span><span style="color: #000000">\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">56</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">57</span>&nbsp;<span style="color: #000000">}</span></div>
<p style="text-indent: 2em">&nbsp;</p><img src ="http://www.cppblog.com/wanghaiguang/aggbug/195119.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-11-13 16:52 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/11/13/195119.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>算法导论——计数排序（网易公开课）</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/11/13/195105.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Tue, 13 Nov 2012 03:07:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/11/13/195105.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/195105.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/11/13/195105.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/195105.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/195105.html</trackback:ping><description><![CDATA[<span style="font-family: Verdana; font-size: 12pt">算法介绍 </span>
<div class="spctrl"></div><span style="font-family: Verdana; font-size: 12pt">　　计数排序是一个类似于桶排序的</span><a href="http://baike.baidu.com/view/297739.htm" target="_blank"><span style="font-family: Verdana; font-size: 12pt">排序算法</span></a><span style="font-family: Verdana; font-size: 12pt">，其优势是对已知数</span><span style="font-family: Verdana; font-size: 12pt">量范围的数组进行排序。它创建一个长度为这个数据范围的数组C，</span><span style="font-family: Verdana; font-size: 12pt">C中每个元素记录要排序数组中对</span><span style="font-family: Verdana; font-size: 12pt">应记录的出现个数。这个算法于1954年由 Harold H. Seward 提出。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>
<p><span style="font-family: Verdana; font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: Verdana; font-size: 12pt">当输入的元素是 n 个 0 到 k 之间的</span><span style="font-family: Verdana; font-size: 12pt">整数时，</span><span style="font-family: Verdana; font-size: 12pt">它的运行时间是 &#920;(</span><em style="font-family: Verdana; font-size: 12pt">n</em><span style="font-size: 12pt"> + </span><em style="font-size: 12pt">k</em><span style="font-size: 12pt">)。计数排序不是</span><a style="font-size: 12pt" title="比较排序" href="http://zh.wikipedia.org/wiki/%E6%AF%94%E8%BE%83%E6%8E%92%E5%BA%8F">比较排序</a><span style="font-size: 12pt">，排序的速度快于任何比较排序算法。</span></p>
<p><span style="font-family: Verdana; font-size: 12pt">由于用来计数的数组</span><em style="font-family: Verdana; font-size: 12pt">C</em><span style="font-size: 12pt">的长度取决于待排序数组中数据的范围（等于待排序数组的最大值与最小值的差加上1</span><span style="font-size: 12pt">），这使得计</span><span style="font-size: 12pt">数排序对于数据范围很大的数组，需要大量时间和内存。例如：计数排序是用来排序0到100之间的数字的最好的算法，但是它不适合按字母顺序排序人名。但是，计数排序可以用在</span><a style="font-size: 12pt" title="基数排序" href="http://zh.wikipedia.org/wiki/%E5%9F%BA%E6%95%B0%E6%8E%92%E5%BA%8F">基数排序</a><span style="font-size: 12pt">中的算法来排序数据范围很大的数组。计数排序之所以能够突破前面所述的&#937;(nlgn)极限，是因为它不是基于元素比较的。计数排序适合所需排序的数组元素取值范围不大的情况(范围太大的话辅助空间很大)。<br /><br /></span><span style="color: red; font-size: 12pt">定理：任意一个比较排序算法在最坏情况下，都需要做&#937;(nlgn)次比较。<span style="font-size: 12pt"></span></p>
<p></span><br /><span style="font-family: Verdana; font-size: 12pt">算法的步骤如下：</span></p>
<ul><li><span style="font-family: Verdana; font-size: 12pt">找出待排序的数</span><span style="font-family: Verdana; font-size: 12pt">组中最大和最小的元素</span></li><li><span style="font-family: Verdana; font-size: 12pt">统计数组中每个值为</span><em style="font-family: Verdana; font-size: 12pt">i</em><span style="font-size: 12pt">的</span><span style="font-size: 12pt">元素出现的次数，存入数组</span><em style="font-size: 12pt">C</em><span style="font-size: 12pt">的第</span><em style="font-size: 12pt">i</em><span style="font-size: 12pt">项</span></li><li><span style="font-family: Verdana; font-size: 12pt">对所有的计数累加（从</span><em style="font-family: Verdana; font-size: 12pt">C</em><span style="font-size: 12pt">中的第一个元素开始，每一项和前一项相加）</span></li><li><span style="font-family: Verdana; font-size: 12pt">反向填充目标数组：将每个元素</span><em style="font-family: Verdana; font-size: 12pt">i</em><span style="font-size: 12pt">放在新数组的第</span><em style="font-size: 12pt">C(i)</em><span style="font-size: 12pt">项，每放一个元素就将</span><em style="font-size: 12pt">C(i)</em><span style="font-size: 12pt">减去1</span></li></ul>
<p><span style="font-family: Verdana; font-size: 12pt">以下引自麻省理工学院算法导论&#8212;&#8212;笔记：</span><br /><br /><span style="font-family: Verdana; font-size: 12pt">Counting sort: No comparisons between elements.</span><br /><span style="font-family: Verdana; font-size: 12pt">&#8226; Input: A[1 . . n], where A[ j]􀂏{1, 2, &#8230;, k} .</span><br /><span style="font-family: Verdana; font-size: 12pt">&#8226; Output: B[1 . . n], sorted.</span><br /><span style="font-family: Verdana; font-size: 12pt">&#8226; Auxiliary storage: C[1 . . k] .</span><br /><br /><span style="font-family: Verdana; font-size: 12pt">Counting sort</span><br /><span style="font-family: Verdana; font-size: 12pt">for i &nbsp;&#8592; 1 to k</span><br /><span style="font-family: Verdana; font-size: 12pt">do C[i] &nbsp;&#8592; 0</span><br /><span style="font-family: Verdana; font-size: 12pt">for j &nbsp;&#8592;1 to n</span><br /><span style="font-family: Verdana; font-size: 12pt">do C[A[ j]] &nbsp;&#8592; C[A[ j]] + 1&nbsp;&nbsp;&nbsp;&#8212;&gt;&nbsp;C[i] = |{key = i}|</span><br /><span style="font-family: Verdana; font-size: 12pt">for i &nbsp;&#8592; 2 to k</span><br /><span style="font-family: Verdana; font-size: 12pt">do C[i] &nbsp;&#8592; C[i] + C[i&#8211;1]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&gt;C[i] = |{key &nbsp;&#8592; i}|</span><br /><span style="font-family: Verdana; font-size: 12pt">for j &nbsp;&#8592; n downto 1</span><br /><span style="font-family: Verdana; font-size: 12pt">do B[C[A[ j]]] &nbsp;&#8592; A[ j]</span><br /><span style="font-family: Verdana; font-size: 12pt">C[A[ j]] &nbsp;&#8592; C[A[ j]] &#8211; 1</span><br /><br /><span style="font-family: Verdana; font-size: 12pt">实例：</span><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/CountingSort1.jpg" width="597" longdesc="" height="1058" /><br /><br /></p>
<p><span style="font-family: Verdana; font-size: 12pt">对所有的计数累加（从</span><em style="font-family: Verdana; font-size: 12pt">C</em><span style="font-family: Verdana; font-size: 12pt">中的第一个元素开始，每一项和前一项相加）</span></p>
<p><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/CountingSort2.jpg" width="588" longdesc="" height="265" /><br /><br /></p>
<p><span style="font-family: Verdana; font-size: 12pt">反向填充目标数组：将每个元素</span><em style="font-family: Verdana; font-size: 12pt">i</em><span style="font-family: Verdana; font-size: 12pt">放在新数组的第</span><em style="font-family: Verdana; font-size: 12pt">C(i)</em><span style="font-family: Verdana; font-size: 12pt">项，每放一个元素就将</span><em style="font-family: Verdana; font-size: 12pt">C(i)</em><span style="font-family: Verdana; font-size: 12pt">减去1</span><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/CountingSort3.jpg" width="592" longdesc="" height="1365" /><br /><br /><span style="color: red"><br /></span><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/CountingSort4.jpg" width="476" longdesc="" height="392" /><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/CountingSort5.jpg" width="555" longdesc="" height="405" /><br /><br /></p>
<p><span style="font-family: Verdana; font-size: 12pt">注：基于比较的排序算法的最佳平均时间复杂度为 O(nlogn)</span><br /><span style="font-family: Verdana; font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;稳定性：算法是稳定的。</span></p>
<p><br /><span style="font-family: Verdana; font-size: 12pt">如果k小于nlogn可以用计数排序，如果k大于nlogn可以用归并排序。</span></p>
<p><br /><span style="font-family: Verdana; font-size: 12pt">代码实例：</span></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #000000">C</span><span style="color: #000000">++</span><span style="color: #000000">实现：<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000"></span><span style="color: #008000">//</span><span style="color: #008000">n为数组元素个数，k是最大的那个元素</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;CountingSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">input,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;size,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;k){<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i;<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">result&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">[size];&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">开辟一个保存结果的临时数组</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">count&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">[k</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">];&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">开辟一个临时数组</span><span style="color: #008000"><br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">k;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">count[i]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000"></span><span style="color: #008000">//</span><span style="color: #008000">使count[i]等于等于i的元素的个数</span><span style="color: #008000"><br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">size;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000"></span><span style="color: #000000">++</span><span style="color: #000000">count[input[i]];&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">count数组中坐标为元素input[i]的增加1，即该元素出现的次数加1</span><span style="color: #008000"><br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">k;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">count[i]&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;count[i</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">size</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">i){&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">正序来也行，但是到这来可以使排序是稳定的</span><span style="color: #008000"><br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">--</span><span style="color: #000000">count[input[i]];&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">因为数组下标从0开始，所以这个放在前面</span><span style="color: #008000"><br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">result[count[input[i]]]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;input[i];&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">这个比较绕，&nbsp;count[input[i]-1]&nbsp;就代表小于等于元素<br /></span><span>&nbsp;</span><span style="color: #000000">&nbsp;</span><span style="color: #008000">&nbsp; </span><span style="color: #000000">}&nbsp;&nbsp; </span><span style="color: #008000"><br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">copy(result,result</span><span style="color: #000000">+</span><span style="color: #000000">size,input);&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">调用copy函数把结果存回原数组</span><span style="color: #008000"><br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">delete&nbsp;[]&nbsp;result;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">记得释放空间</span><span style="color: #008000"><br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">delete&nbsp;[]&nbsp;count;<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;input[</span><span style="color: #000000">11</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">{</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">7</span><span style="color: #000000">,</span><span style="color: #000000">4</span><span style="color: #000000">,</span><span style="color: #000000">9</span><span style="color: #000000">,</span><span style="color: #000000">8</span><span style="color: #000000">,</span><span style="color: #000000">5</span><span style="color: #000000">,</span><span style="color: #000000">7</span><span style="color: #000000">,</span><span style="color: #000000">8</span><span style="color: #000000">,</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #000000">7</span><span style="color: #000000">};<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000">CountingSort(input,</span><span style="color: #000000">11</span><span style="color: #000000">,</span><span style="color: #000000">9</span><span style="color: #000000">);<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">11</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000">printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">,input[i]);<br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #000000">putchar(</span><span style="color: #000000">'</span><span style="color: #000000">\n</span><span style="color: #000000">'</span><span style="color: #000000">);<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #000000">}</span></div>
<p>&nbsp;</p><img src ="http://www.cppblog.com/wanghaiguang/aggbug/195105.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-11-13 11:07 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/11/13/195105.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）优秀博客推荐：各种数据结构与算法知识入门经典（不断更新) </title><link>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180884.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Sat, 30 Jun 2012 08:21:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180884.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/180884.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180884.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/180884.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/180884.html</trackback:ping><description><![CDATA[<span style="background-color: #ffff00">基本算法</span><br />贪心算法：<a href="http://www.cnblogs.com/chinazhangjie/archive/2010/11/23/1885330.html"><font color="#1a8bc8">贪心算法</font></a> 作者：<span style="color: #ff0000" class="Apple-style-span">独酌逸醉<br /><a href="http://www.cppblog.com/3522021224/archive/2007/06/16/26429.aspx"><font color="#1a8bc8">贪心算法精讲</font></a>&nbsp;</span><span style="color: #000000" class="Apple-style-span">作者：<span style="line-height: 18px; font-family: Arial; white-space: nowrap; color: #ff0000" class="Apple-style-span"><strong>3522021224</strong></span><br /></span>递归和分治：<a title="递归与分治策略" href="http://blog.csdn.net/zhoudaxia/article/details/5765084" target="_blank"><font color="#1a8bc8">递归与分治策略</font></a> 作者：<span style="line-height: 13px; font-family: Arial, Console, Verdana, 'Courier New'; color: #ff0000; font-weight: bold" class="Apple-style-span">zhoudaxia</span><br /><br /><span style="background-color: #ffff00">图论</span><br />图的遍历（DFS和BFS）： <a href="http://jefferent.iteye.com/blog/1122934"><font color="#1a8bc8">图的遍历</font></a> 作者：<span style="line-height: 18px; font-family: Helvetica, Tahoma, Arial, sans-serif; color: red" class="Apple-style-span">jefferent</span><br />最小生成树（Prim算法和Kruskal算法）： <a href="http://www.cnblogs.com/chinazhangjie/archive/2010/12/02/1894314.html" target="_blank"><font color="#1a8bc8">贪心算法--最小生成树</font></a> 作者：<span style="color: red">独酌逸醉</span><br /><span style="line-height: 25px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; color: #333333">Dijkstra算法： <a title="最短路径之Dijkstra算法详细讲解  " href="http://2728green-rock.blog.163.com/blog/static/43636790200901211848284/" target="_blank"><font color="#1a8bc8">最短路径之Dijkstra算法详细讲解</font></a> 作者：</span><span style="cursor: pointer"><a style="cursor: pointer" href="http://blog.163.com/2728green-rock/" target="_blank" m2a?=""><font class="Apple-style-span" color="#3c910d"><span style="line-height: normal" class="Apple-style-span">绿岩<br /></span></font></a></span><a title="最短路径算法&#8212;Dijkstra(迪杰斯特拉)算法分析与实现(C/C++)" href="http://www.wutianqi.com/?p=1890"><font color="#1a8bc8">最短路径算法&#8212;Dijkstra(迪杰斯特拉)算法分析与实现(C/C++)</font></a> 作者：<span style="line-height: 13px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; color: #ff0000" class="Apple-style-span">tankywoo</span><br /><span style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53" class="Apple-style-span"><span style="line-height: 25px">Bellman-Ford</span></span><span style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53" class="Apple-style-span"><span style="line-height: 25px">算法：<a title="最短路径算法&#8212;Bellman-Ford(贝尔曼-福特)算法分析与实现(C/C++)" href="http://www.wutianqi.com/?p=1912" target="_blank"><font color="#1a8bc8">最短路径算法&#8212;Bellman-Ford(贝尔曼-福特)算法分析与实现(C/C++)</font></a> 作者：<span style="line-height: 15px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; color: red" class="Apple-style-span">tankywoo</span><br /></span></span><span style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; color: #333333" class="Apple-style-span">Floyd-Warshall算法：<a title="最短路径算法&#8212;Floyd(弗洛伊德)算法分析与实现(C/C++)" href="http://www.wutianqi.com/?p=1903"><font color="#1a8bc8">最短路径算法&#8212;Floyd(弗洛伊德)算法分析与实现(C/C++)</font></a> 作者：<span style="line-height: 13px; font-family: 'Lucida Grande', Arial, Helvetica, sans-serif; color: #ff0000" class="Apple-style-span">tankywoo</span><br /></span><span style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; color: #333333" class="Apple-style-span">Johnson算法：<a title="Johnson 算法" href="http://blog.csdn.net/huliang82/article/details/4166588"><font color="#1a8bc8">Johnson 算法</font></a> 作者：<span style="line-height: 13px; font-family: Arial, Console, Verdana, 'Courier New'; color: red; font-weight: bold" class="Apple-style-span">huliang82</span><br /></span><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, simsun, u5b8bu4f53"><span style="line-height: 24px" class="Apple-style-span">A*算法：</span></font><a style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; color: #333333" title="A*算法详解" href="http://yangxy84.blog.163.com/blog/static/70844302009313114846776/">A*算法详解</a><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, simsun, u5b8bu4f53"><span style="line-height: 24px" class="Apple-style-span"> 作者：</span></font><span style="line-height: normal; font-family: verdana, 'courier new'; color: #444444" class="Apple-style-span"><a style="color: #936e3f; cursor: pointer; text-decoration: none" href="http://blog.163.com/yangxy84/" target="_blank" m2a?=""><span style="color: red">愚人有节</span></a></span><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, simsun, u5b8bu4f53"><span style="line-height: 24px" class="Apple-style-span"><br /></span></font><span style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; color: #333333" class="Apple-style-span">拓扑排序：<a title="拓扑排序" href="http://blog.csdn.net/midgard/article/details/4101025"><font color="#1a8bc8">拓扑排序 </font></a>作者： 
<div style="display: inline !important"><span style="line-height: 14px; font-family: Arial, Console, Verdana, 'Courier New'; color: red; font-size: 12px; font-weight: bold">midgard</span></div><br /></span><a title="如何去理解 拓扑排序算法" href="http://www.cnblogs.com/shanyou/archive/2006/11/16/562861.html"><font color="#1a8bc8">如何去理解 拓扑排序算法</font></a> 作者：<span style="line-height: normal; font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; color: red" class="Apple-style-span"><strong style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px">张善友</strong></span><br /><span style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; color: #333333" class="Apple-style-span">关键路径：<a title="关键路径" href="http://www.cnblogs.com/navorse/articles/1893863.html" target="_blank"><font color="#1a8bc8">关键路径</font></a> 作者：<span style="line-height: 16px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: #000000" class="Apple-style-span"><a style="padding-bottom: 1px; font-style: italic; padding-left: 1px; padding-right: 1px; color: navy; font-size: 11px; text-decoration: none; padding-top: 1px" href="http://home.cnblogs.com/u/navorse/"><span style="color: red">navorse</span></a></span><br /></span><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, simsun, u5b8bu4f53"><span style="line-height: 24px" class="Apple-style-span">欧拉路：</span></font><a style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; color: #333333" title="欧拉路问题" href="http://hi.baidu.com/luyade1987/blog/item/f2304d0fd6b4922f6059f3ab.html" target="_blank">欧拉路问题</a><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, simsun, u5b8bu4f53"><span style="line-height: 24px" class="Apple-style-span"> 作者：</span></font><span style="line-height: 24px; font-family: tahoma, arial, 宋体, sans-serif; color: #000000" class="Apple-style-span"><a style="color: #5fa207; text-decoration: none" href="http://hi.baidu.com/luyade1987/home"><span style="color: red; font-size: 14px">MaiK</span></a></span><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, simsun, u5b8bu4f53"><span style="line-height: 24px" class="Apple-style-span"><br /></span></font><span style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; color: #333333" class="Apple-style-span">差分约束：<a title="差分约束系统" href="http://fuliang.iteye.com/blog/368214"><font color="#1a8bc8">差分约束系统</font></a> 作者：<span style="line-height: 18px; font-family: Helvetica, Tahoma, Arial, sans-serif; color: red; font-weight: bold" class="Apple-style-span">fuliang</span><br /></span><span style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; color: #333333" class="Apple-style-span">二分图最大匹配：<a title="二分图匹配总结" href="http://starforever.blog.hexun.com/3963571_d.html" target="_blank"><font color="#1a8bc8">二分图匹配总结</font></a> 作者：<span style="line-height: 36px; font-family: Simsun; color: #3b3b3b" class="Apple-style-span"><a style="background-image: none; color: #0048aa; text-decoration: none; background-origin: initial; background-clip: initial" title="访问北极天南星的个人门户" href="http://hexun.com/starforever/default.html" target="_blank"><span style="color: red">北极天南星<br /></span></a></span><a href="http://blog.163.com/baobao_zhang@126/blog/static/482523672008631103625967/" target="_blank"><font color="#1a8bc8">二分图匹配算法总结</font></a> 作者：<span style="line-height: normal; color: #000000" class="Apple-style-span"><a style="color: #2886e4; cursor: pointer; text-decoration: none" href="http://blog.163.com/baobao_zhang@126/" target="_blank" m2a?=""><span style="color: red">z7m8v6</span></a></span><br /></span><span style="line-height: 24px; font-family: Arial, Helvetica, simsun, u5b8bu4f53; color: #333333" class="Apple-style-span">网络流：<a title="网络流基础" href="http://chhaj5236.blog.163.com/blog/static/1128810812009827112857794/" target="_blank"><font color="#1a8bc8">网络流基础</font></a> 作者：<span style="line-height: normal; font-family: verdana, 'courier new'; color: #000000" class="Apple-style-span"><span style="color: red; cursor: pointer">chhaj523</span></span><br /></span><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, simsun, u5b8bu4f53"><span style="line-height: 24px" class="Apple-style-span"><br /></span></font><span style="background-color: yellow"><font class="Apple-style-span" color="#333333" face="Arial, Helvetica, simsun, u5b8bu4f53"><span style="line-height: 25px" class="Apple-style-span">数据结构<br /><span style="line-height: 19px; background-color: #ffffff; font-family: verdana, 'courier new'; color: #000000" class="Apple-style-span">并查集：<a title="并查集--学习详解" href="http://www.cnblogs.com/cherish_yimi/archive/2009/10/11/1580839.html"><font color="#1a8bc8">并查集--学习详解</font></a> 作者：<span style="font-family: 宋体; color: red" class="Apple-style-span">yx_th000</span><br /></span></span></font></span><span style="line-height: 19px" class="Apple-style-span">哈希表：<a title="哈希表" href="http://www.cnblogs.com/jiewei915/archive/2010/08/09/1796042.html"><font color="#1a8bc8">哈希表</font></a> 作者：</span><span style="line-height: 19px; color: red" class="Apple-style-span">猎人杰</span><span style="line-height: 19px" class="Apple-style-span"></span><br /><span style="line-height: 19px" class="Apple-style-span">二分查找：<a title="查找（二）：二分查找" href="http://www.cnblogs.com/xiaosuo/archive/2010/04/07/1687231.html"><font color="#1a8bc8">查找（二）：二分查找</font></a> 作者：<span style="line-height: 21px; font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; color: #88866f" class="Apple-style-span"><a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: #464646; text-decoration: none; padding-top: 0px" href="http://home.cnblogs.com/u/xiaosuo/"><span style="color: red">xiaosuo</span></a></span><br /></span><span style="line-height: 19px" class="Apple-style-span">哈夫曼树：</span><a style="line-height: 19px" title="哈夫曼树" href="http://www.cppblog.com/Cass/archive/2011/10/02/157353.aspx"><font color="#1a8bc8">哈夫曼树</font></a><span style="line-height: 19px" class="Apple-style-span"> 作者：</span><span style="line-height: 19px; color: red" class="Apple-style-span">angle<span style="color: #000000" class="Apple-style-span">平衡二叉树： <a href="http://www.cnblogs.com/fornever/archive/2011/11/15/2249492.html" target="_blank"><font color="#1a8bc8">平衡二叉树（解惑）</font></a> 作者：<span style="color: red">Never</span></span><br /></span>树状数组：<a href="http://hi.baidu.com/%D0%DC%C3%A8yingcai/blog/item/b329184ef0aa85f5d62afce1.html" target="_blank"><font color="#1a8bc8">树状数组总结</font></a> 作者：<span style="line-height: 18px; font-family: Arial; white-space: nowrap; color: red" class="Apple-style-span">熊猫yingcai</span><br />线段树： <a href="http://duanple.blog.163.com/blog/static/70971767200922110494318" target="_blank"><font color="#1a8bc8">线段树总结</font></a> 作者：<span style="color: red">星星</span><br />归并排序求逆序数：<a title="利用归并排序求逆序数" href="http://www.cppblog.com/jake1036/archive/2011/04/06/143531.html"><font color="#1a8bc8">利用归并排序求逆序数</font></a> 作者：<a style="line-height: 19px; background-color: #ffffff; font-family: verdana, arial, sans-serif; color: #999966; font-size: 12px" href="http://www.cppblog.com/jake1036/"><span style="color: red">kahn</span></a><br /><span style="background-color: #ffff00">动态规划（DP）</span><br />简单动态规划：<a title="动态规划" href="http://www.cnblogs.com/brokencode/archive/2011/06/26/2090702.html"><font color="#1a8bc8">动态规划</font></a> 作者：<span style="line-height: 18px; font-family: verdana, 宋体, Arial; color: #333333" class="Apple-style-span"><a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: #333333; text-decoration: none; padding-top: 0px" href="http://home.cnblogs.com/u/brokencode/"><span style="color: red">brokencode</span></a></span><br />背包问题：《背包九讲》<br /><br /><br /><span style="background-color: #ffff00">数学</span><br />遗传算法： <a title="遗传算法入门" href="http://www.cnblogs.com/heaad/archive/2010/12/23/1914725.html"><font color="#1a8bc8">遗传算法入门</font></a> 作者：<span style="color: red">heaad</span><br />容斥原理：<a title="容斥原理（翻译）" href="http://www.cppblog.com/vici/archive/2011/09/05/155103.html" target="_blank"><font color="#1a8bc8">容斥原理（翻译）</font></a> 作者：<span style="text-align: right; line-height: 20px; background-color: #ffffff; font-family: verdana, Arial, helvetica, sans-seriff; color: red; font-size: 13px">vici</span><font color="#333333" face="Arial"><span style="line-height: 24px"><br /></span></font><span style="line-height: 24px; font-family: Arial; color: #333333" class="Apple-style-span">母函数：<a title="母函数入门小结" href="http://blog.csdn.net/zhangxiang0125/article/details/6177930" target="_blank"><font color="#1a8bc8">母函数入门小结</font></a> 作者：</span><span style="text-align: center; line-height: 14px; background-color: #ffffff; font-family: Arial, Console, Verdana, 'Courier New'; color: red; font-size: 12px; font-weight: bold">zhangxiang0125<br /></span><span style="line-height: 24px; font-family: Arial; color: #333333" class="Apple-style-span">秦九韶算法：<a title="秦九韶算法" href="http://blog.csdn.net/simonezhlx/article/details/5443714"><font color="#1a8bc8">秦九韶算法</font></a> 作者：</span><span style="text-align: center; line-height: 14px; background-color: #ffffff; font-family: Arial, Console, Verdana, 'Courier New'; color: red; font-size: 12px; font-weight: bold">simonezhlx</span><span style="line-height: 24px; font-family: Arial; color: #333333" class="Apple-style-span"><br /></span><span style="line-height: 24px; font-family: Arial; color: #333333" class="Apple-style-span">高斯消元法：</span><br /><span style="line-height: 24px; font-family: Arial; color: #333333" class="Apple-style-span">欧几里得定理（GCD）：</span><br /><span style="line-height: 24px; font-family: Arial; color: #333333" class="Apple-style-span">扩展欧几里得定理：</span><br /><span style="line-height: 24px; font-family: Arial; color: #333333" class="Apple-style-span">中国剩余定理：</span><br /><span style="line-height: 24px; font-family: Arial; color: #333333" class="Apple-style-span">概率问题：</span><br /><font class="Apple-style-span" color="#ff0000"><br /></font><span style="background-color: #ffff00">计算几何</span><br />几何公式：<br />离散化： <a href="http://www.matrix67.com/blog/archives/108" target="_blank"><font color="#1a8bc8">什么是离散化？</font></a> 作者：<span style="color: red">matrix67</span><br />扫描线算法：<br />叉积和点积：<br />凸包： <br /><br />本文转自：<a href="http://www.cppblog.com/cxiaojia/archive/2011/11/16/rumen.html">http://www.cppblog.com/cxiaojia/archive/2011/11/16/rumen.html</a><img src ="http://www.cppblog.com/wanghaiguang/aggbug/180884.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-06-30 16:21 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180884.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最短路径算法—Floyd(弗洛伊德)算法分析与实现(C/C++)</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180883.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Sat, 30 Jun 2012 08:18:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180883.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/180883.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180883.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/180883.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/180883.html</trackback:ping><description><![CDATA[<p>Floyd-Warshall算法，简称<a href="http://www.wutianqi.com/?p=1903" target="_blank">Floyd算法</a>，用于<strong>求解任意两点间的最短距离，时间复杂度为O(n^3)。</strong></p>
<p><strong>使用条件&amp;范围</strong><br />通常可以在任何图中使用，包括有向图、带负权边的图。</p>
<p>Floyd-Warshall 算法用来找出每对点之间的最短距离。它需要用邻接矩阵来储存边，这个算法通过考虑最佳子路径来得到最佳路径。 </p>
<p>1.注意单独一条边的路径也不一定是最佳路径。<br />2.从任意一条单边路径开始。所有两点之间的距离是边的权，或者无穷大，如果两点之间没有边相连。<br />对于每一对顶点 u 和 v，看看是否存在一个顶点 w 使得从 u 到 w 再到 v 比己知的路径更短。如果是更新它。<br />3.不可思议的是，只要按排适当，就能得到结果。<br />伪代码:</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #008000">//</span><span style="color: #008000">&nbsp;dist(i,j)&nbsp;为从节点i到节点j的最短距离</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">For&nbsp;i&#8592;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;to&nbsp;n&nbsp;</span><span style="color: #0000ff">do</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;For&nbsp;j&#8592;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;to&nbsp;n&nbsp;</span><span style="color: #0000ff">do</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist(i,j)&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;weight(i,j)&nbsp;<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">For&nbsp;k&#8592;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;to&nbsp;n&nbsp;</span><span style="color: #0000ff">do</span><span style="color: #000000">&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;k为&#8220;媒介节点&#8221;</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;For&nbsp;i&#8592;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;to&nbsp;n&nbsp;</span><span style="color: #0000ff">do</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For&nbsp;j&#8592;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;to&nbsp;n&nbsp;</span><span style="color: #0000ff">do</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(dist(i,k)&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;dist(k,j)&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;dist(i,j))&nbsp;then&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;是否是更短的路径？</span><span style="color: #008000"><br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist(i,j)&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;dist(i,k)&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;dist(k,j)</span></div>
<p>我们平时所见的<strong>Floyd算法的一般形式</strong>如下：</p>
<div class="wp_syntax"></div>
<p>&nbsp;</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">1</span>&nbsp;<span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Floyd(){<br /></span><span style="color: #008080">2</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,k;<br /></span><span style="color: #008080">3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(k</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;k</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;k</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(dist[i][k]</span><span style="color: #000000">+</span><span style="color: #000000">dist[k][j]</span><span style="color: #000000">&lt;</span><span style="color: #000000">dist[i][j])<br /></span><span style="color: #008080">7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i][j]</span><span style="color: #000000">=</span><span style="color: #000000">dist[i][k]</span><span style="color: #000000">+</span><span style="color: #000000">dist[k][j];<br /></span><span style="color: #008080">8</span>&nbsp;<span style="color: #000000">}</span></div>
<p>注意下第6行这个地方，如果dist[i][k]或者dist[k][j]不存在，程序中用一个很大的数代替。最好写成if(dist[i] [k]!=INF &amp;&amp; dist[k][j]!=INF &amp;&amp; dist[i][k]+dist[k][j]<dist[i][j])，从而防止溢出所造成的错误。< p=""></dist[i][j])，从而防止溢出所造成的错误。<> 
<p>Floyd算法的实现以及输出最短路径和最短路径长度，具体过程请看【<a href="http://www.ycrc.com.cn/qinghua/23/flash/chap07/7-5-1.swf" target="_blank">动画演示Floyd算法</a>】。</p>
<p>代码说明几点：</p>
<p>1、A[][]数组初始化为各顶点间的原本距离，最后存储各顶点间的最短距离。</p>
<p>2、path[][]数组保存最短路径，与当前迭代的次数有关。初始化都为-1，表示没有中间顶点。在求A[i][j]过程中，path[i][j]存放从顶点vi到顶点vj的中间顶点编号不大于k的最短路径上前一个结点的编号。在算法结束时，由二维数组path的值回溯，可以得到从顶点vi到顶点vj的最短路径。</p>
<p>初始化A[][]数组为如下，即有向图的邻接矩阵。</p>
<p><a class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2011/01/floyd1.jpg"></a></p>
<p><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/image/floyd1.jpg" width="260" height="426" /><br /><br />完整的<a href="http://www.wutianqi.com/?p=1903" target="_blank"><strong>实现代码</strong></a>如下：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;&nbsp;1</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;&nbsp;2</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">string</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;&nbsp;3</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;&nbsp;4</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;&nbsp;5</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MaxVertexNum&nbsp;100&nbsp;&nbsp;&nbsp;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;&nbsp;6</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;INF&nbsp;32767&nbsp;&nbsp;&nbsp;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;&nbsp;7</span>&nbsp;<span style="color: #000000">typedef&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;&nbsp;8</span>&nbsp;<span style="color: #000000">{&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;vertex[MaxVertexNum];&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;edges[MaxVertexNum][MaxVertexNum];&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,e;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;12</span>&nbsp;<span style="color: #000000">}MGraph;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;13</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;14</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;CreateMGraph(MGraph&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">G)&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;15</span>&nbsp;<span style="color: #000000">{&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,k,p;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">请输入顶点数和边数:</span><span style="color: #000000">"</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">G.n</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">G.e;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">请输入顶点元素:</span><span style="color: #000000">"</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">G.n;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">G.vertex[i];&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">G.n;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;25</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;26</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">G.n;j</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;27</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;28</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;G.edges[i][j]</span><span style="color: #000000">=</span><span style="color: #000000">INF;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;29</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">==</span><span style="color: #000000">j)&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;30</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;31</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;G.edges[i][j]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;32</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;33</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;34</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;35</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(k</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;k</span><span style="color: #000000">&lt;</span><span style="color: #000000">G.e;k</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;36</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;37</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">请输入第</span><span style="color: #000000">"</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">k</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">条弧头弧尾序号和相应的权值:</span><span style="color: #000000">"</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;38</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">i</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">j</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">p;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;39</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;G.edges[i][j]</span><span style="color: #000000">=</span><span style="color: #000000">p;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;40</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;41</span>&nbsp;<span style="color: #000000">}&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;42</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Dispath(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;A[][MaxVertexNum],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;path[][MaxVertexNum],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n);<br /></span><span style="color: #008080">&nbsp;43</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;44</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Floyd(MGraph&nbsp;G)<br /></span><span style="color: #008080">&nbsp;45</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;46</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;A[MaxVertexNum][MaxVertexNum],path[MaxVertexNum][MaxVertexNum];<br /></span><span style="color: #008080">&nbsp;47</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j,k;<br /></span><span style="color: #008080">&nbsp;48</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">G.n;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;49</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;50</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">G.n;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;51</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;52</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A[i][j]</span><span style="color: #000000">=</span><span style="color: #000000">G.edges[i][j];<br /></span><span style="color: #008080">&nbsp;53</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path[i][j]</span><span style="color: #000000">=-</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;54</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;55</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;56</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(k</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;k</span><span style="color: #000000">&lt;</span><span style="color: #000000">G.n;k</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;57</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;58</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">G.n;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;59</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;60</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">G.n;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;61</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;62</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(A[i][j]</span><span style="color: #000000">&gt;</span><span style="color: #000000">A[i][k]</span><span style="color: #000000">+</span><span style="color: #000000">A[k][j])<br /></span><span style="color: #008080">&nbsp;63</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;64</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A[i][j]</span><span style="color: #000000">=</span><span style="color: #000000">A[i][k]</span><span style="color: #000000">+</span><span style="color: #000000">A[k][j];<br /></span><span style="color: #008080">&nbsp;65</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path[i][j]</span><span style="color: #000000">=</span><span style="color: #000000">k;<br /></span><span style="color: #008080">&nbsp;66</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;67</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;68</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;69</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;70</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;Dispath(A,path,G.n);<br /></span><span style="color: #008080">&nbsp;71</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">&nbsp;72</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;73</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Ppath(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;path[][MaxVertexNum],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j)<br /></span><span style="color: #008080">&nbsp;74</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;75</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;k;<br /></span><span style="color: #008080">&nbsp;76</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;k</span><span style="color: #000000">=</span><span style="color: #000000">path[i][j];<br /></span><span style="color: #008080">&nbsp;77</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(k</span><span style="color: #000000">==-</span><span style="color: #000000">1</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;78</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;79</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;80</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;81</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;Ppath(path,i,k);<br /></span><span style="color: #008080">&nbsp;82</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d,</span><span style="color: #000000">"</span><span style="color: #000000">,k);<br /></span><span style="color: #008080">&nbsp;83</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;Ppath(path,k,j);<br /></span><span style="color: #008080">&nbsp;84</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">&nbsp;85</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;86</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Dispath(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;A[][MaxVertexNum],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;path[][MaxVertexNum],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n)<br /></span><span style="color: #008080">&nbsp;87</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;88</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j;<br /></span><span style="color: #008080">&nbsp;89</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;90</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;91</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;92</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;93</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(A[i][j]</span><span style="color: #000000">==</span><span style="color: #000000">INF)<br /></span><span style="color: #008080">&nbsp;94</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;95</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">!=</span><span style="color: #000000">j)<br /></span><span style="color: #008080">&nbsp;96</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;97</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">从%d到%d没有路径\n</span><span style="color: #000000">"</span><span style="color: #000000">,i,j);<br /></span><span style="color: #008080">&nbsp;98</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;99</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">100</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /></span><span style="color: #008080">101</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">102</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;&nbsp;从%d到%d=&gt;路径长度:%d路径:</span><span style="color: #000000">"</span><span style="color: #000000">,i,j,A[i][j]);<br /></span><span style="color: #008080">103</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d,</span><span style="color: #000000">"</span><span style="color: #000000">,i);<br /></span><span style="color: #008080">104</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ppath(path,i,j);<br /></span><span style="color: #008080">105</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,j);<br /></span><span style="color: #008080">106</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">107</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">108</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">109</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">110</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">111</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">112</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">113</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000">"</span><span style="color: #000000">input2.txt</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">r</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;stdin);<br /></span><span style="color: #008080">114</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;MGraph&nbsp;G;<br /></span><span style="color: #008080">115</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CreateMGraph(G);<br /></span><span style="color: #008080">116</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;Floyd(G);<br /></span><span style="color: #008080">117</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">118</span>&nbsp;<span style="color: #000000">}</span></div>
<p>测试结果如下：</p>
<p><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/image/floyd2.jpg" width="668" height="511" /><br /><br />本文转自：<a href="http://www.wutianqi.com/?p=1903">http://www.wutianqi.com/?p=1903</a><a class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2011/01/floyd2.jpg"></a></p><img src ="http://www.cppblog.com/wanghaiguang/aggbug/180883.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-06-30 16:18 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180883.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）最短路径算法—Dijkstra(迪杰斯特拉)算法分析与实现(C/C++)</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180881.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Sat, 30 Jun 2012 08:12:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180881.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/180881.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180881.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/180881.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/180881.html</trackback:ping><description><![CDATA[<br />Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法，用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展，直到扩展到终点为止。<a href="http://www.wutianqi.com/?p=1890" target="_blank">Dijkstra算法</a>能得出最短路径的最优解，但由于它遍历计算的节点很多，所以效率低。 
<p>Dijkstra算法是很有代表性的最短路算法，在很多专业课程中都作为基本内容有详细的介绍，如数据结构，图论，运筹学等等。</p>
<p>其基本思想是，设置顶点集合S并不断地作贪心选择来扩充这个集合。一个顶点属于集合S当且仅当从源到该顶点的最短路径长度已知。<br /><span id="more-1890"></span></p>
<p><span style="color: rgb(255,102,0)">初始时，S中仅含有源。设u是G的某一个顶点，把从源到u且中间只经过S中顶点的路称为从源到u的<span style="color: rgb(255,0,0)">特殊路径</span>，并用数组dist记录当前每个顶点所对应的最短特殊路径长度。Dijkstra算法每次从V-S中取出具有最短特殊路长度的顶点u，将u添加到S中，同时对数组dist作必要的修改。一旦S包含了所有V中顶点，dist就记录了从源到所有其它顶点之间的最短路径长度。</span></p>
<p>例如，对下图中的有向图，应用<a href="http://www.wutianqi.com/?p=1890" target="_blank">Dijkstra算法</a>计算从源顶点1到其它顶点间最短路径的过程列在下表中。</p>
<p><a class="highslide-image" href="http://www.wutianqi.com/wp-content/uploads/2011/01/dijkstra1.jpg"></a>Dijkstra算法的迭代过程：<br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/image/dijkstra1.jpg" width="293" height="334" /><br /><br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/wanghaiguang/image/dijkstra2.jpg" width="500" height="200" /><br /></p>
<p>&nbsp;</p>
<p><strong>主题好好理解上图！</strong></p>
<p>以下是具体的实现(C/C++):</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;&nbsp;1</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;&nbsp;2</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;&nbsp;4</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;maxnum&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;&nbsp;5</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;maxint&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">999999</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;&nbsp;7</span>&nbsp;<span style="color: #000000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;各数组都从下标1开始</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;&nbsp;8</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;dist[maxnum];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;表示当前点到源点的最短路径长度</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;&nbsp;9</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;prev[maxnum];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;记录当前点的前一个结点</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;10</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;c[maxnum][maxnum];&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;记录图的两点间路径长度</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;11</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,&nbsp;line;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;图的结点数和路径数<br /></span><span style="color: #008080">&nbsp;12</span>&nbsp;<span style="color: #008000">&nbsp;<br /></span><span style="color: #008080">&nbsp;13</span>&nbsp;<span style="color: #008000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;n&nbsp;--&nbsp;n&nbsp;nodes<br /></span><span style="color: #008080">&nbsp;14</span>&nbsp;<span style="color: #008000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;v&nbsp;--&nbsp;the&nbsp;source&nbsp;node<br /></span><span style="color: #008080">&nbsp;15</span>&nbsp;<span style="color: #008000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;dist[]&nbsp;--&nbsp;the&nbsp;distance&nbsp;from&nbsp;the&nbsp;ith&nbsp;node&nbsp;to&nbsp;the&nbsp;source&nbsp;node<br /></span><span style="color: #008080">&nbsp;16</span>&nbsp;<span style="color: #008000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;prev[]&nbsp;--&nbsp;the&nbsp;previous&nbsp;node&nbsp;of&nbsp;the&nbsp;ith&nbsp;node<br /></span><span style="color: #008080">&nbsp;17</span>&nbsp;<span style="color: #008000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;c[][]&nbsp;--&nbsp;every&nbsp;two&nbsp;nodes'&nbsp;distance</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;18</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Dijkstra(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;v,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">dist,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">prev,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;c[maxnum][maxnum])<br /></span><span style="color: #008080">&nbsp;19</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;s[maxnum];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;判断是否已存入该点到S集合中</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;21</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">&nbsp;22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;c[v][i];<br /></span><span style="color: #008080">&nbsp;24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;初始都未用过该点</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;25</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(dist[i]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;maxint)<br /></span><span style="color: #008080">&nbsp;26</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prev[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;27</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;28</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prev[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;v;<br /></span><span style="color: #008080">&nbsp;29</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;30</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;dist[v]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;31</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;s[v]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;32</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;33</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;依次将未放入S集合的结点中，取dist[]最小值的结点，放入结合S中<br /></span><span style="color: #008080">&nbsp;34</span>&nbsp;<span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;一旦S包含了所有V中顶点，dist就记录了从源点到所有其他顶点之间的最短路径长度<br /></span><span style="color: #008080">&nbsp;35</span>&nbsp;<span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;注意是从第二个节点开始，第一个为源点</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;36</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">2</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">&nbsp;37</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;38</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;tmp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;maxint;<br /></span><span style="color: #008080">&nbsp;39</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;u&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;v;<br /></span><span style="color: #008080">&nbsp;40</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;找出当前未使用的点j的dist[j]最小值</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;41</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /></span><span style="color: #008080">&nbsp;42</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">((</span><span style="color: #000000">!</span><span style="color: #000000">s[j])&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;dist[j]</span><span style="color: #000000">&lt;</span><span style="color: #000000">tmp)<br /></span><span style="color: #008080">&nbsp;43</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;44</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;j;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;u保存当前邻接点中距离最小的点的号码</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;45</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;dist[j];<br /></span><span style="color: #008080">&nbsp;46</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;47</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s[u]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;表示u点已存入S集合中<br /></span><span style="color: #008080">&nbsp;48</span>&nbsp;<span style="color: #008000">&nbsp;<br /></span><span style="color: #008080">&nbsp;49</span>&nbsp;<span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;更新dist</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;50</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /></span><span style="color: #008080">&nbsp;51</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">((</span><span style="color: #000000">!</span><span style="color: #000000">s[j])&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;c[u][j]</span><span style="color: #000000">&lt;</span><span style="color: #000000">maxint)<br /></span><span style="color: #008080">&nbsp;52</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;53</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;newdist&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;dist[u]&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;c[u][j];<br /></span><span style="color: #008080">&nbsp;54</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(newdist&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;dist[j])<br /></span><span style="color: #008080">&nbsp;55</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;56</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;newdist;<br /></span><span style="color: #008080">&nbsp;57</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prev[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;u;<br /></span><span style="color: #008080">&nbsp;58</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;59</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;60</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;61</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">&nbsp;62</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;63</span>&nbsp;<span style="color: #000000"></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;查找从源点v到终点u的路径，并输出</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;64</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;searchPath(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">prev,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;v,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;u)<br /></span><span style="color: #008080">&nbsp;65</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;66</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;que[maxnum];<br /></span><span style="color: #008080">&nbsp;67</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;tot&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;68</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;que[tot]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;u;<br /></span><span style="color: #008080">&nbsp;69</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;70</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;tmp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;prev[u];<br /></span><span style="color: #008080">&nbsp;71</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(tmp&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;v)<br /></span><span style="color: #008080">&nbsp;72</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;73</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;que[tot]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;tmp;<br /></span><span style="color: #008080">&nbsp;74</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;75</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;prev[tmp];<br /></span><span style="color: #008080">&nbsp;76</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">&nbsp;77</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;que[tot]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;v;<br /></span><span style="color: #008080">&nbsp;78</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">tot;&nbsp;i</span><span style="color: #000000">&gt;=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">i)<br /></span><span style="color: #008080">&nbsp;79</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(i&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;80</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;que[i]&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;-&gt;&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;81</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;82</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;que[i]&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">&nbsp;83</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">&nbsp;84</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;85</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">&nbsp;86</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;87</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000">"</span><span style="color: #000000">input.txt</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">r</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;stdin);<br /></span><span style="color: #008080">&nbsp;88</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;各数组都从下标1开始<br /></span><span style="color: #008080">&nbsp;89</span>&nbsp;<span style="color: #008000">&nbsp;<br /></span><span style="color: #008080">&nbsp;90</span>&nbsp;<span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;输入结点数</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;91</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cin&nbsp;</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">&nbsp;n;<br /></span><span style="color: #008080">&nbsp;92</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;输入路径数</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;93</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cin&nbsp;</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">&nbsp;line;<br /></span><span style="color: #008080">&nbsp;94</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;p,&nbsp;q,&nbsp;len;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;输入p,&nbsp;q两点及其路径长度<br /></span><span style="color: #008080">&nbsp;95</span>&nbsp;<span style="color: #008000">&nbsp;<br /></span><span style="color: #008080">&nbsp;96</span>&nbsp;<span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;初始化c[][]为maxint</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;97</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">&nbsp;98</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /></span><span style="color: #008080">&nbsp;99</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[i][j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;maxint;<br /></span><span style="color: #008080">100</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">101</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">line;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)&nbsp;&nbsp;<br /></span><span style="color: #008080">102</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">103</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin&nbsp;</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">&nbsp;p&nbsp;</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">&nbsp;q&nbsp;</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">&nbsp;len;<br /></span><span style="color: #008080">104</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(len&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;c[p][q])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;有重边</span><span style="color: #008000"><br /></span><span style="color: #008080">105</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">106</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[p][q]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;len;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;p指向q</span><span style="color: #008000"><br /></span><span style="color: #008080">107</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[q][p]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;len;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;q指向p，这样表示无向图</span><span style="color: #008000"><br /></span><span style="color: #008080">108</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">109</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">110</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">111</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">112</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;maxint;<br /></span><span style="color: #008080">113</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">114</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">115</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /></span><span style="color: #008080">116</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%8d</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;c[i][j]);<br /></span><span style="color: #008080">117</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">118</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">119</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">120</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;Dijkstra(n,&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;dist,&nbsp;prev,&nbsp;c);<br /></span><span style="color: #008080">121</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">122</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;最短路径长度</span><span style="color: #008000"><br /></span><span style="color: #008080">123</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">源点到最后一个顶点的最短路径长度:&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;dist[n]&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">124</span>&nbsp;<span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">125</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;路径</span><span style="color: #008000"><br /></span><span style="color: #008080">126</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">源点到最后一个顶点的路径为:&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">;<br /></span><span style="color: #008080">127</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;searchPath(prev,&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;n);<br /></span><span style="color: #008080">128</span>&nbsp;<span style="color: #000000">}</span></div>
<p>输入数据:<br />5<br />7<br />1 2 10<br />1 4 30<br />1 5 100<br />2 3 50<br />3 5 10<br />4 3 20<br />4 5 60<br />输出数据:<br /><span style="color: rgb(255,255,255)"><span style="background-color: rgb(0,0,0)">999999 10 999999 30 100<br />10 999999 50 999999 999999<br />999999 50 999999 20 10<br />30 999999 20 999999 60<br />100 999999 10 60 999999<br />源点到最后一个顶点的最短路径长度: 60<br />源点到最后一个顶点的路径为: 1 -&gt; 4 -&gt; 3 -&gt; 5<br /><br /></span><span style="background-color: rgb(0,0,0)">&nbsp;本文转自：<a href="http://www.wutianqi.com/?p=1890">http://www.wutianqi.com/?p=1890</a><br />其他连接：<a href="http://2728green-rock.blog.163.com/blog/static/43636790200901211848284/">http://2728green-rock.blog.163.com/blog/static/43636790200901211848284/</a></span><span style="background-color: rgb(0,0,0)"><br /></span></span></p><img src ="http://www.cppblog.com/wanghaiguang/aggbug/180881.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-06-30 16:12 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180881.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>贪心算法</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180879.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Sat, 30 Jun 2012 07:55:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180879.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/180879.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180879.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/180879.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/180879.html</trackback:ping><description><![CDATA[<p><strong><span>一．贪心算法的基本概念</span>
<object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="32" height="32">
<param name="_cx" value="846" /><param name="_cy" value="846" /><param name="FlashVars" value="" /><param name="Movie" value="yuyingflash3/18t1.swf" /><param name="Src" value="yuyingflash3/18t1.swf" /><param name="WMode" value="Window" /><param name="Play" value="0" /><param name="Loop" value="-1" /><param name="Quality" value="High" /><param name="SAlign" value="" /><param name="Menu" value="-1" /><param name="Base" value="" /><param name="AllowScriptAccess" value="" /><param name="Scale" value="ShowAll" /><param name="DeviceFont" value="0" /><param name="EmbedMovie" value="0" /><param name="BGColor" value="" /><param name="SWRemote" value="" /><param name="MovieData" value="" /><param name="SeamlessTabbing" value="1" /><param name="Profile" value="0" /><param name="ProfileAddress" value="" /><param name="ProfilePort" value="0" /><param name="AllowNetworking" value="all" /><param name="AllowFullScreen" value="false" /><param name="AllowFullScreenInteractive" value="false" />
<embed src="yuyingflash3/18t1.swf" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_version=shockwaveflash" type="application/x-shockwave-flash" width="52" height="26">      </embed>    
</object></strong></p>
<p><span></span>当一个问题具有最优子结构性质时，我们会想到用动态规划法去解它。但有时会有更简单有效的算法。我们来看一个找硬币的例子。假设有四种硬币，它们的面值分别为二角五分、一角、五分和一分。现在要找给某顾客六角三分钱。这时，我们会不假思索地拿出2个二角五分的硬币，1个一角的硬币和3个一分的硬币交给顾客。这种找硬币方法与其他的找法相比，所拿出的硬币个数是最少的。这里，我们下意识地使用了这样的找硬币算法：首先选出一个面值不超过六角三分的最大硬币，即二角五分；然后从六角三分中减去二角五分，剩下三角八分；再选出一个面值不超过三角八分的最大硬币，即又一个二角五分，如此一直做下去。这个找硬币的方法实际上就是贪心算法。顾名思义，贪心算法总是作出在当前看来是最好的选择。也就是说贪心算法并不从整体最优上加以考虑，它所作出的选择只是在某种意义上的局部最优选择。当然，我们希望贪心算法得到的最终结果也是整体最优的。上面所说的找硬币算法得到的结果就是一个整体最优解。找硬币问题本身具有最优子结构性质，它可以用动态规划算法来解。但我们看到，用贪心算法更简单，更直接且解题效率更高。这利用了问题本身的一些特性。例如，上述找硬币的算法利用了硬币面值的特殊性。如果硬币的面值改为一分、五分和一角一分3种，而要找给顾客的是一角五分钱。还用贪心算法，我们将找给顾客1个一角一分的硬币和4个一分的硬币。然而3个五分的硬币显然是最好的找法。虽然贪心算法不是对所有问题都能得到整体最优解，但对范围相当广的许多问题它能产生整体最优解。如图的单源最短路径问题，最小生成树问题等。在一些情况下，即使贪心算法不能得到整体最优解，但其最终结果却是最优解的很好的近似解。</p>
<p><strong><span>二．求解活动安排问题算法</span>
<object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="32" height="32">
<param name="_cx" value="846" /><param name="_cy" value="846" /><param name="FlashVars" value="" /><param name="Movie" value="yuyingflash3/18t2.swf" /><param name="Src" value="yuyingflash3/18t2.swf" /><param name="WMode" value="Window" /><param name="Play" value="0" /><param name="Loop" value="-1" /><param name="Quality" value="High" /><param name="SAlign" value="" /><param name="Menu" value="-1" /><param name="Base" value="" /><param name="AllowScriptAccess" value="" /><param name="Scale" value="ShowAll" /><param name="DeviceFont" value="0" /><param name="EmbedMovie" value="0" /><param name="BGColor" value="" /><param name="SWRemote" value="" /><param name="MovieData" value="" /><param name="SeamlessTabbing" value="1" /><param name="Profile" value="0" /><param name="ProfileAddress" value="" /><param name="ProfilePort" value="0" /><param name="AllowNetworking" value="all" /><param name="AllowFullScreen" value="false" /><param name="AllowFullScreenInteractive" value="false" />
                                                                                 
                       <embed src="yuyingflash3/18t2.swf" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_version=shockwaveflash" type="application/x-shockwave-flash" width="32" height="32">      
</embed>        </object></strong></p>
<p><span></span>活动安排问题是可以用贪心算法有效求解的一个很好的例子。该问题要求高效地安排一系列争用某一公共资源的活动。贪心算法提供了一个简单、漂亮的方法使得尽可能多的活动能兼容地使用公共资源。</p>
<p><span></span>设有n个活动的集合e={1，2，&#8230;，n}，其中每个活动都要求使用同一资源，如演讲会场等，而在同一时间内只有一个活动能使用这一资源。每个活动i都有一个要求使用该资源的起始时间si和一个结束时间f<sub>i</sub>,且s<sub>i</sub>&lt;f<sub>i</sub>。如果选择了活动i，则它在半开时间区间[s<sub>i</sub>，f<sub>i</sub>]内占用资源。若区间[s<sub>i</sub>，f<sub>i</sub>]与区间[s<sub>j</sub>，f<sub>j</sub>]不相交，则称活动i与活动j是相容的。也就是说，当s<sub>i</sub>&#8805;fi或s<sub>j</sub>&#8805;f<sub>j</sub>时，活动i与活动j相容。活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合。</p>
<p><span></span>在下面所给出的解活动安排问题的贪心算法gpeedyselector中，各活动的起始时间和结束时间存储于数组s和f{中且按结束时间的非减序：．f1&#8804;f2&#8804;&#8230;&#8804;fn排列。如果所给出的活动未按此序排列，我们可以用o(nlogn)的时间将它重排。<strong></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #000000">template</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;type</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;greedyselector(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,&nbsp;type&nbsp;s[&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;type&nbsp;f[&nbsp;],&nbsp;</span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;a[&nbsp;]&nbsp;]<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">2</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">n;i</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;)&nbsp;<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(s[i]</span><span style="color: #000000">&gt;=</span><span style="color: #000000">f[j])&nbsp;<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">i;<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i]</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">}</span></div>
<p><br />算法greedyselector</strong>中用集合a来存储所选择的活动。活动i在集合a中，当且仅当a[i]的值为true。变量j用以记录最近一次加入到a中的活动。由于输入的活动是按其结束时间的非减序排列的，f<sub>j</sub>总是当前集合a中所有活动的最大结束时间，即：</p>
<p><span></span><sub></sub></p>
<p><span></span>贪心算法greedyselector一开始选择活动1，并将j初始化为1。然后依次检查活动i是否与当前已选择的所有活动相容。若相容则将活动i加人到已选择活动的集合a中，否则不选择活动i，而继续检查下一活动与集合a中活动的相容性。由于f<sub>i</sub></p>
<p>总是当前集合<span>a中所有活动的最大结束时间，故活动i与当前集合a中所有活动相容的充分且必要的条件是其开始时间s<sub> </sub>不早于最近加入集合a中的活动j的结束时间f<sub>j</sub>，s<sub>i</sub>&#8805;f<sub>j</sub>。若活动i与之相容，则i成为最近加人集合a中的活动，因而取代活动j的位置。由于输人的活动是以其完成时间的非减序排列的，所以算法greedyselector每次总是选择具有最早完成时间的相容活动加入集合a中。直观上按这种方法选择相容活动就为未安排活动留下尽可能多的时间。也就是说，该算法的贪心选择的意义是使剩余的可安排时间段极大化，以便安排尽可能多的相容活动。算法greedyselector的效率极高。当输人的活动已按结束时间的非减序排列，算法只需g(n)的时间来安排n个活动，使最多的活动能相容地使用公共资源。</span></p>
<p>例：设待安排的<span>11个活动的开始时间和结束时间按结束时间的非减序排列如下：</span></p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="48">
<p><span>i</span></p></td>
<td valign="top" width="48">
<p><span>1</span></p></td>
<td valign="top" width="48">
<p><span>2</span></p></td>
<td valign="top" width="48">
<p><span>3</span></p></td>
<td valign="top" width="48">
<p><span>4</span></p></td>
<td valign="top" width="48">
<p><span>5</span></p></td>
<td valign="top" width="48">
<p><span>6</span></p></td>
<td valign="top" width="48">
<p><span>7</span></p></td>
<td valign="top" width="48">
<p><span>8</span></p></td>
<td valign="top" width="48">
<p><span>9</span></p></td>
<td valign="top" width="48">
<p><span>10</span></p></td>
<td valign="top" width="48">
<p><span>11</span></p></td></tr>
<tr>
<td valign="top" width="48">
<p><span>s[i]</span></p></td>
<td valign="top" width="48">
<p><span>1</span></p></td>
<td valign="top" width="48">
<p><span>3</span></p></td>
<td valign="top" width="48">
<p><span>0</span></p></td>
<td valign="top" width="48">
<p><span>5</span></p></td>
<td valign="top" width="48">
<p><span>3</span></p></td>
<td valign="top" width="48">
<p><span>5</span></p></td>
<td valign="top" width="48">
<p><span>6</span></p></td>
<td valign="top" width="48">
<p><span>8</span></p></td>
<td valign="top" width="48">
<p><span>8</span></p></td>
<td valign="top" width="48">
<p><span>2</span></p></td>
<td valign="top" width="48">
<p><span>12</span></p></td></tr>
<tr>
<td valign="top" width="48">
<p><span>f[i]</span></p></td>
<td valign="top" width="48">
<p><span>4</span></p></td>
<td valign="top" width="48">
<p><span>5</span></p></td>
<td valign="top" width="48">
<p><span>6</span></p></td>
<td valign="top" width="48">
<p><span>7</span></p></td>
<td valign="top" width="48">
<p><span>8</span></p></td>
<td valign="top" width="48">
<p><span>9</span></p></td>
<td valign="top" width="48">
<p><span>10</span></p></td>
<td valign="top" width="48">
<p><span>11</span></p></td>
<td valign="top" width="48">
<p><span>12</span></p></td>
<td valign="top" width="48">
<p><span>13</span></p></td>
<td valign="top" width="48">
<p><span>14</span></p></td></tr></tbody></table>
<p><span></span></p>
<p>算法<span>greedyselector的计算过程如图所示。</span></p>
<p>&nbsp;</p>
<p><span></span>图中每行相应于算法的一次迭代。阴影长条表示的活动是已选人集合a中的活动，而空白长条表示的活动是当前正在检查其相容性的活动。<br /></p>
<p class="p0"><span style="font-family: 宋体"><img alt="" src="http://pic002.cnblogs.com/images/2010/153357/2010112312542337.jpg" width="343" height="400" /></span></p>
<p><br />若被检查的活动i的开始时间s<sub>i</sub>小于最近选择的活动了的结束时间f<sub>j</sub>，则不选择活动i，否则选择活动i加入集合a中。</p>
<p><strong><span>三．算法分析</span>
<object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="52" height="26">
<param name="_cx" value="1375" /><param name="_cy" value="687" /><param name="FlashVars" value="" /><param name="Movie" value="yuyingflash3/18t3.swf" /><param name="Src" value="yuyingflash3/18t3.swf" /><param name="WMode" value="Window" /><param name="Play" value="0" /><param name="Loop" value="-1" /><param name="Quality" value="High" /><param name="SAlign" value="" /><param name="Menu" value="-1" /><param name="Base" value="" /><param name="AllowScriptAccess" value="" /><param name="Scale" value="ShowAll" /><param name="DeviceFont" value="0" /><param name="EmbedMovie" value="0" /><param name="BGColor" value="" /><param name="SWRemote" value="" /><param name="MovieData" value="" /><param name="SeamlessTabbing" value="1" /><param name="Profile" value="0" /><param name="ProfileAddress" value="" /><param name="ProfilePort" value="0" /><param name="AllowNetworking" value="all" /><param name="AllowFullScreen" value="false" /><param name="AllowFullScreenInteractive" value="false" />
                                                                                 
                                                                                 
                                              <embed src="yuyingflash3/18t3.swf" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_version=shockwaveflash" type="application/x-shockwave-flash" width="52" height="26">      
</embed>            </object></strong></p>
<p><span></span>贪心算法并不总能求得问题的整体最优解。但对于活动安排问题，贪心算法greedyse&#8212;1ector却总能求得的整体最优解，即它最终所确定的相容活动集合a的规模最大。我们可以用数学归纳法来证明这个结论。</p>
<p><span></span>事实上，设e={1，2，&#8230;，n}为所给的活动集合。由于正中活动按结束时间的非减序排列，故活动1具有最早的完成时间。首先我们要证明活动安排问题有一个最优解以贪心选择开始，即该最优解中包含活动1。设<sub> </sub>是所给的活动安排问题的一个最优解，且a中活动也按结束时间非减序排列，a中的第一个活动是活动k。若k=1，则a就是一个以贪心选择开始的最优解。若k&gt;1，则我们设<sub> </sub>。由于f1&#8804;f<sub>k</sub>，且a中活动是互为相容的，故b中的活动也是互为相容的。又由于b中活动个数与a中活动个数相同，且a是最优的，故b也是最优的。也就是说b是一个以贪心选择活动1开始的最优活动安排。因此，我们证明了总存在一个以贪心选择开始的最优活动安排方案。</p>
<p><span></span>进一步，在作了贪心选择，即选择了活动1后，原问题就简化为对e中所有与活动1相容的活动进行活动安排的子问题。即若a是原问题的一个最优解，则a<span>&#8217;</span><span>=a&#8212;{i}是活动安排问题<sub> </sub>的一个最优解。事实上，如果我们能找到e</span><span>&#8217;</span>的一个解<span>b</span><span>&#8217;</span>，它包含比<span>a</span><span>&#8217;</span>更多的活动，则将活动<span>1加入到b</span><span>&#8217;</span>中将产生<span>e的一个解b，它包含比a更多的活动。这与a的最优性矛盾。因此，每一步所作的贪心选择都将问题简化为一个更小的与原问题具有相同形式的子问题。对贪心选择次数用数学归纳法即知，贪心算法greedyselector最终产生原问题的一个最优解。</span></p>
<p><strong><span>四．贪心算法的基本要素</span>
<object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="32" height="32">
<param name="_cx" value="846" /><param name="_cy" value="846" /><param name="FlashVars" value="" /><param name="Movie" value="yuyingflash3/18t4.swf" /><param name="Src" value="yuyingflash3/18t4.swf" /><param name="WMode" value="Window" /><param name="Play" value="0" /><param name="Loop" value="-1" /><param name="Quality" value="High" /><param name="SAlign" value="" /><param name="Menu" value="-1" /><param name="Base" value="" /><param name="AllowScriptAccess" value="" /><param name="Scale" value="ShowAll" /><param name="DeviceFont" value="0" /><param name="EmbedMovie" value="0" /><param name="BGColor" value="" /><param name="SWRemote" value="" /><param name="MovieData" value="" /><param name="SeamlessTabbing" value="1" /><param name="Profile" value="0" /><param name="ProfileAddress" value="" /><param name="ProfilePort" value="0" /><param name="AllowNetworking" value="all" /><param name="AllowFullScreen" value="false" /><param name="AllowFullScreenInteractive" value="false" />
                                                                                 
                                                                                 
                                                                                 
                                                                     <embed src="yuyingflash3/18t4.swf" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_version=shockwaveflash" type="application/x-shockwave-flash" width="32" height="32">      
</embed>                </object></strong></p>
<p><span></span>贪心算法通过一系列的选择来得到一个问题的解。它所作的每一个选择都是当前状态下某种意义的最好选择，即贪心选择。希望通过每次所作的贪心选择导致最终结果是问题的一个最优解。这种启发式的策略并不总能奏效，然而在许多情况下确能达到预期的目的。解活动安排问题的贪心算法就是一个例子。下面我们着重讨论可以用贪心算法求解的问题的一般特征。</p>
<p><span></span>对于一个具体的问题，我们怎么知道是否可用贪心算法来解此问题，以及能否得到问题的一个最优解呢?这个问题很难给予肯定的回答。但是，从许多可以用贪心算法求解的问题中</p>
<p>我们看到它们一般具有两个重要的性质：<strong>贪心选择性质</strong>和<strong>最优子结构性质</strong>。</p>
<p><span>1．<strong>贪心选择性质</strong></span></p>
<p><span></span><em>所谓贪心选择性质是指所求问题的整体最优解可以通过一系列局部最优的选择，即贪心选择来达到</em>。这是贪心算法可行的第一个基本要素，也是贪心算法与动态规划算法的主要区别。在动态规划算法中，每步所作的选择往往依赖于相关子问题的解。因而只有在解出相关子问题后，才能作出选择。而在贪心算法中，仅在当前状态下作出最好选择，即局部最优选择。然后再去解作出这个选择后产生的相应的子问题。贪心算法所作的贪心选择可以依赖于以往所作过的选择，但决不依赖于将来所作的选择，也不依赖于子问题的解。正是由于这种差别，动态规划算法通常以自底向上的方式解各子问题，而贪心算法则通常以自顶向下的方式进行,以迭代的方式作出相继的贪心选择，每作一次贪心选择就将所求问题简化为一个规模更小的子问题。</p>
<p>对于一个具体问题，要确定它是否具有贪心选择性质，我们必须证明每一步所作的贪心选择最终导致问题的一个整体最优解。通常可以用我们在证明活动安排问题的贪心选择性质时所采用的方法来证明。首先考察问题的一个整体最优解，并证明可修改这个最优解，使其以贪心选择开始。而且作了贪心选择后，原问题简化为一个规模更小的类似子问题。然后，用数学归纳法证明，通过每一步作贪心选择，最终可得到问题的一个整体最优解。其中，证明贪心选择后的问题简化为规模更小的类似子问题的关键在于利用该问题的最优子结构性质。</p>
<p><span>2．<strong>最优子结构性质</strong></span></p>
<p><span></span>当一个问题的最优解包含着它的子问题的最优解时，称此问题具有最优子结构性质。问题所具有的这个性质是该问题可用动态规划算法或贪心算法求解的一个关键特征。在活动安排问题中，其最优子结构性质表现为：若a是对于正的活动安排问题包含活动1的一个最优解,则相容活动集合a<span>&#8217;</span><span>=a&#8212;{1}是对于e</span><span>&#8217;</span><span>={i&#8712;e:s<sub>i</sub>&#8805;f<sub>1</sub>}的活动安排问题的一个最优解。</span></p>
<p><span>3．<strong>贪心算法与动态规划算法的差异</strong></span></p>
<p><span></span>贪心算法和动态规划算法都要求问题具有最优子结构性质，这是两类算法的一个共同点。但是，对于一个具有最优子结构的问题应该选用贪心算法还是动态规划算法来求解?是不是能用动态规划算法求解的问题也能用贪心算法来求解?下面我们来研究两个经典的组合优化问题，并以此来说明贪心算法与动态规划算法的主要差别。</p>
<p><strong><span>五．</span></strong> <strong><span>０－背包问题</span>
<object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="52" height="26">
<param name="_cx" value="1375" /><param name="_cy" value="687" /><param name="FlashVars" value="" /><param name="Movie" value="yuyingflash3/18t5.swf" /><param name="Src" value="yuyingflash3/18t5.swf" /><param name="WMode" value="Window" /><param name="Play" value="0" /><param name="Loop" value="-1" /><param name="Quality" value="High" /><param name="SAlign" value="" /><param name="Menu" value="-1" /><param name="Base" value="" /><param name="AllowScriptAccess" value="" /><param name="Scale" value="ShowAll" /><param name="DeviceFont" value="0" /><param name="EmbedMovie" value="0" /><param name="BGColor" value="" /><param name="SWRemote" value="" /><param name="MovieData" value="" /><param name="SeamlessTabbing" value="1" /><param name="Profile" value="0" /><param name="ProfileAddress" value="" /><param name="ProfilePort" value="0" /><param name="AllowNetworking" value="all" /><param name="AllowFullScreen" value="false" /><param name="AllowFullScreenInteractive" value="false" />
                                                                                 
                                                                                 
                                                                                 
                                                                                 
                                                                                 
           <embed src="yuyingflash3/18t5.swf" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_version=shockwaveflash" type="application/x-shockwave-flash" width="52" height="26">  
    </embed>                    </object></strong></p>
<p>给定<span>n种物品和一个背包。物品i的重量是w<sub> </sub>，其价值为v<sub> </sub>，背包的容量为c.问应如何选择装入背包中的物品，使得装入背包中物品的总价值最大? 在选择装入背包的物品时，对每种物品i只有两种选择，即装入背包或不装入背包。不能将物品i装入背包多次，也不能只装入部分的物品i。</span></p>
<p><span></span>此问题的形式化描述是，给定c&gt;0，w<sub>i</sub>&gt;0，v<sub>i</sub>&gt;0，1&#8804;i&#8804;n，要求找出一个n元0&#8212;1向</p>
<p>量<span>(xl，x2，&#8230;，x<sub>n</sub>)，<sub> </sub>，使得<sub> </sub>&#8804;c，而且<sub> </sub>达到最大。</span></p>
<p><span></span>背包问题：与0-1背包问题类似，所不同的是在选择物品i装入背包时，可以选择物品i的一部分，而不一定要全部装入背包。</p>
<p><span></span>此问题的形式化描述是，给定c&gt;0，w<sub>i</sub>&gt;0，v<sub>i</sub>&gt;0，1&#8804;i&#8804;n，要求找出一个n元向量</p>
<p><span>(x1,x2,...x<sub>n</sub>)，0&#8804;x<sub>i</sub>&#8804;1，1&#8804;i&#8804;n 使得<sub> </sub>&#8804;c，而且<sub> </sub>达到最大。</span></p>
<p><span></span>这两类问题都具有最优子结构性质。对于0&#8212;1背包问题，设a是能够装入容量为c的背包的具有最大价值的物品集合，则a<sub>j</sub>=a-{j}是n-1个物品1，2，&#8230;，j&#8212;1，j+1，&#8230;，n可装入容量为c-w<sub>i</sub>叫的背包的具有最大价值的物品集合。对于背包问题，类似地，若它的一个最优解包含物品j，则从该最优解中拿出所含的物品j的那部分重量w<sub>i</sub>，剩余的将是n-1个原重物品1，2，&#8230;，j-1，j+1，&#8230;，n以及重为w<sub>j</sub>-w<sub>i</sub>的物品j中可装入容量为c-w的背包且具有最大价值的物品。</p>
<p><span></span>虽然这两个问题极为相似，但背包问题可以用贪心算法求解，而0&#183;1背包问题却不能用贪心算法求解。用贪心算法解背包问题的基本步骤是，首先计算每种物品单位重量的价值</p>
<p><span>v<sub>j</sub>／w<sub>i</sub>然后，依贪心选择策略，将尽可能多的单位重量价值最高的物品装入背包。若将这种物品全部装入背包后，背包内的物品总重量未超过c，则选择单位重量价值次高的物品并尽可能多地装入背包。依此策略一直进行下去直到背包装满为止。具体算法可描述如下：</span></p>
<p><span>void knapsack(int n, float m, float v[ ], float w[ ], float x[ ] )</span></p>
<p><span>sort(n,v,w);</span></p>
<p><span>int i;</span></p>
<p><span>for(i= 1;i&lt;= n;i++) x[i] = o;</span></p>
<p><span>float c = m;</span></p>
<p><span>for (i = 1;i &lt; = n;i ++) {</span></p>
<p><span></span>if (w[i] &gt; c) break;</p>
<p><span></span>x[i] = 1;</p>
<p><span></span>c-= w[i];</p>
<p><span></span>}</p>
<p><span>if (i &lt; = n) x[i] = c/w[i];</span></p>
<p><span>} </span></p>
<p>算法<span>knapsack的主要计算时间在于将各种物品依其单位重量的价值从大到小排序。因此，算法的计算时间上界为o(nlogn)。当然，为了证明算法的正确性，我们还必须证明背包问题具有贪心选择性质。 </span></p>
<p>这种贪心选择策略对<span>0&#8212;1背包问题就不适用了。看图2(a)中的例子，背包的容量为50千克；物品1重10千克；价值60元；物品2重20千克，价值100元；物品3重30千克；价值120元。因此，物品1每千克价值6元，物品2每千克价值5元，物品3每千克价值4元。若依贪心选择策略，应首选物品1装入背包，然而从图4&#8212;2(b)的各种情况可以看出，最优的选择方案是选择物品2和物品3装入背包。首选物品1的两种方案都不是最优的。对于背包问题，贪心选择最终可得到最优解，其选择方案如图2(c)所示。</span></p>
<p>&nbsp;</p>
<p><span></span>对于0&#8212;1背包问题，贪心选择之所以不能得到最优解是因为它无法保证最终能将背包装满，部分背包空间的闲置使每千克背包空间所具有的价值降低了。事实上，在考虑0&#8212;1背包问题的物品选择时，应比较选择该物品和不选择该物品所导致的最终结果，然后再作出最好选择。由此就导出许多互相重叠的于问题。这正是该问题可用动态规划算法求解的另一重要特征。动态规划算法的确可以有效地解0&#8212;1背包问题。<br /><br />本文转自：<a href="http://www.cppblog.com/3522021224/archive/2007/06/16/26429.aspx">http://www.cppblog.com/3522021224/archive/2007/06/16/26429.aspx</a><br />其他链接：<a href="http://www.cnblogs.com/chinazhangjie/archive/2010/11/23/1885330.html">http://www.cnblogs.com/chinazhangjie/archive/2010/11/23/1885330.html</a></p><img src ="http://www.cppblog.com/wanghaiguang/aggbug/180879.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-06-30 15:55 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/06/30/180879.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>哈希查找</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/05/28/176500.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Mon, 28 May 2012 07:54:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/05/28/176500.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/176500.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/05/28/176500.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/176500.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/176500.html</trackback:ping><description><![CDATA[<div>本文转自：<a href="http://www.cnblogs.com/jillzhang/archive/2006/11/02/547679.html">http://www.cnblogs.com/jillzhang/archive/2006/11/02/547679.html</a><br /><br />
<p>哈希表和哈希函数是大学数据结构中的课程，实际开发中我们经常用到Hashtable这种结构，当遇到键-值对存储，采用Hashtable比ArrayList查找的性能高。为什么呢？我们在享受高性能的同时，需要付出什么代价(这几天看红顶商人胡雪岩，经典台词：<span style="color: #ff6600">在你享受这之前，必须受别人吃不了的苦，忍受别人受不了的屈辱</span>)，那么使用Hashtable是否就是一桩无本万利的买卖呢？就此疑问，做以下分析，希望能抛砖引玉。<br /><strong>1)hash它为什么对于键-值查找性能高</strong><br />学过数据结构的，都应该晓得，线性表和树中，记录在结构中的相对位置是随机的，记录和关键字之间不存在明确的关系，因此在查找记录的时候，需要进行一系列的关键字比较，这种查找方式建立在比较的基础之上，在.net中(Array,ArrayList,List)这些集合结构采用了上面的存储方式。<br />比如，现在我们有一个班同学的数据，包括姓名，性别，年龄，学号等。假如数据有<br /><br /></p>
<table style="width: 320px; border-collapse: collapse" border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>学号</td></tr>
<tr>
<td>张三</td>
<td>男</td>
<td>15</td>
<td>1</td></tr>
<tr>
<td>李四</td>
<td>女</td>
<td>14</td>
<td>2</td></tr>
<tr>
<td>王五</td>
<td>男</td>
<td>14</td>
<td>3</td></tr></tbody></table>
<p>&nbsp;</p>
<p>假如，我们按照姓名来查找，假设查找函数FindByName(string name);<br />1)查找&#8220;张三&#8221;<br />只需在第一行匹配一次。<br />2)查找"王五"<br />&nbsp;&nbsp; 在第一行匹配，失败，<br />&nbsp;&nbsp; 在第二行匹配，失败，<br />&nbsp;&nbsp; 在第三行匹配，成功<br />上面两种情况，分别分析了最好的情况，和最坏的情况，那么平均查找次数应该为 (1+3)/2=2次，即平均查找次数为(记录总数+1)的1/2。<br />尽管有一些优化的算法，可以使查找排序效率增高，但是复杂度会保持在log2n的范围之内。<br />如何更更快的进行查找呢？我们所期望的效果是一下子就定位到要找记录的位置之上，这时候时间复杂度为1，查找最快。如果我们事先为每条记录编一个序号，然后让他们按号入位，我们又知道按照什么规则对这些记录进行编号的话，如果我们再次查找某个记录的时候，只需要先通过规则计算出该记录的编号，然后根据编号，在记录的线性队列中，就可以轻易的找到记录了 。<br />注意，上述的描述包含了两个概念，一个是用于对学生进行编号的规则，在数据结构中，称之为哈希函数，另外一个是按照规则为学生排列的顺序结构，称之为哈希表。<br />仍以上面的学生为例，假设学号就是规则，老师手上有一个规则表，在排座位的时候也按照这个规则来排序，查找李四，首先该教师会根据规则判断出，李四的编号为2，就是在座位中的2号位置，直接走过去，&#8220;李四，哈哈，你小子，就是在这！&#8221;<br />看看大体流程:<br />&nbsp; <img border="0" alt="" src="http://images.cnblogs.com/cnblogs_com/jillzhang/dssaw23sdas.JPG" width="600" height="384" /><br />从上面的图中，可以看出哈希表可以描述为两个筒子，一个筒子用来装记录的位置编号，另外一个筒子用来装记录，另外存在一套规则，用来表述记录与编号之间的联系。这个规则通常是如何制定的呢？<br /><strong>a)直接定址法:<br /></strong>&nbsp;&nbsp; 我在前一篇文章对GetHashCode()性能比较的问题中谈到，对于整形的数据GetHashCode()函数返回的就是整形　　　本身，其实就是基于直接定址的方法，比如有一组0-100的数据，用来表示人的年龄<br />那么，采用直接定址的方法构成的哈希表为:<br /></p>
<table style="width: 320px; border-collapse: collapse" border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td></tr>
<tr>
<td>0岁</td>
<td>１岁</td>
<td>２岁</td>
<td>３岁</td>
<td>４岁</td>
<td>５岁</td></tr></tbody></table>.....<br />这样的一种定址方式，简单方便，适用于元数据能够用数字表述或者原数据具有鲜明顺序关系的情形。<br /><strong>b)数字分析法:<br /></strong>&nbsp; 有这样一组数据，用于表述一些人的出生日期<br />
<table style="width: 320px; border-collapse: collapse" border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td>年</td>
<td>月</td>
<td>日</td></tr>
<tr>
<td>７５</td>
<td>１０</td>
<td>１</td></tr>
<tr>
<td>７５</td>
<td>１２</td>
<td>１０</td></tr>
<tr>
<td>７５</td>
<td>０２</td>
<td>１４</td></tr></tbody></table>分析一下，年和月的第一位数字基本相同，造成冲突的几率非常大，而后面三位差别比较大，所以采用后三位<br /><strong>c)平方取中法<br /></strong>　取关键字平方后的中间几位作为哈希地址<br /><strong>d) 折叠法：<br /></strong>　将关键字分割成位数相同的几部分，最后一部分位数可以不相同，然后去这几部分的叠加和（取出进位）作为哈希地址，比如有这样的数据20-1445-4547-3<br />可以<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5473<br />+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4454<br />+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 201<br />=&nbsp;&nbsp;&nbsp;&nbsp;10128<br />取出进位1,取0128为哈希地址<br /><strong>e)取余法<br /></strong>取关键字被某个不大于哈希表表长m的数p除后所得余数为哈希地址。H(key)=key MOD p (p&lt;=m)<br /><strong>f) 随机数法<br /></strong>　选择一个随机函数，取关键字的随机函数值为它的哈希地址，即H(key)=random(key) ,其中random为随机函数。通常用于关键字长度不等时采用此法。<br />
<p>&nbsp;</p>
<p>总之，哈希函数的规则是：通过某种转换关系，使关键字适度的分散到指定大小的的顺序结构中。越分散，则以后查找的时间复杂度越小，空间复杂度越高。<br /><strong>２)使用hash，我们付出了什么？<br /></strong>hash是一种典型以空间换时间的算法，比如原来一个长度为100的数组，对其查找，只需要遍历且匹配相应记录即可，从空间复杂度上来看，假如数组存储的是byte类型数据，那么该数组占用100byte空间。现在我们采用hash算法，我们前面说的hash必须有一个规则，约束键与存储位置的关系，那么就需要一个固定长度的hash表，此时，仍然是100byte的数组，假设我们需要的100byte用来记录键与位置的关系，那么总的空间为200byte,而且用于记录规则的表大小会根据规则，大小可能是不定的，比如在lzw算法中，如果一个很长的用于记录像素的byte数组，用来记录位置与键关系的表空间，算法推荐为一个1２bit能表述的整数大小，那么足够长的像素数组，如何分散到这样定长的表中呢，lzw算法采用的是可变长编码，具体会在深入介绍lzw算法的时候介绍。<br />注:hash表最突出的问题在于冲突，就是两个键值经过哈希函数计算出来的索引位置很可能相同，这个问题，下篇文章会令作阐述。<br />注:之所以会简单得介绍了hash，是为了更好的学习lzw算法，学习lzw算法是为了更好的研究gif文件结构，最后，我将详细的阐述一下gif文件是如何构成的，如何高效操作此种类型文件。<br /><br /><strong style="font-size: 12pt">HASH如何处理冲突</strong>：<br /><br /><strong>1)冲突是如何产生的？<br /></strong>上文中谈到，哈希函数是指如何对关键字进行编址的规则，这里的关键字的范围很广，可视为无限集，如何保证无限集的原数据在编址的时候不会出现重复呢？规则本身无法实现这个目的。举一个例子，仍然用班级同学做比喻，现有如下同学数据<br />张三，李四，王五，赵刚，吴露.....<br />假如我们编址规则为取姓氏中姓的开头字母在字母表的相对位置作为地址，则会产生如下的哈希表<br />
<table border="1" rules="all" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td width="35">位置</td>
<td width="35">字母</td>
<td width="35">姓名</td>
<td width="35"></td></tr>
<tr>
<td width="35">0</td>
<td width="35">a</td>
<td></td>
<td></td></tr>
<tr>
<td>1</td>
<td>b</td>
<td></td>
<td></td></tr>
<tr>
<td>2</td>
<td>c</td>
<td></td>
<td></td></tr></tbody></table></p>
<p>&nbsp;</p>
<p>...<br />
<table border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td width="35">10&nbsp;&nbsp;&nbsp;</td>
<td width="35">L&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td width="35">李四</td>
<td width="35"></td></tr></tbody></table><br />...<br />
<table style="background-color: #c0c0c0" border="1" cellspacing="0" cellpadding="3" BACKGROUND-COLOR: #c0c0c0?>
<tbody>
<tr>
<td width="35">22</td>
<td width="35">W</td>
<td width="100">王五，吴露</td>
<td width="35"></td></tr></tbody></table>..<br />
<table style="background-color: #c0c0c0" border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td width="35">25&nbsp;</td>
<td width="35">Z&nbsp;</td>
<td width="100">张三，赵刚</td>
<td style="background-color: #c0c0c0" width="35"></td></tr></tbody></table><br />我们注意到，灰色背景标示的两行里面，关键字王五，吴露被编到了同一个位置，关键字张三，赵刚也被编到了同一个位置。老师再拿号来找张三，座位上有两个人，"你们俩谁是张三？"<br /><strong>2)如何解决冲突问题<br /></strong>既然不能避免冲突，那么如何解决冲突呢，显然需要附加的步骤。通过这些步骤，以制定更多的规则来管理关键字集合，通常的办法有:<br /><strong>a)开放地址法</strong><br />开放地执法有一个公式:Hi=(H(key)+di) MOD m i=1,2,...,k(k&lt;=m-1)<br />其中，m为哈希表的表长。di 是产生冲突的时候的增量序列。如果di值可能为1,2,3,...m-1，称线性探测再散列。<br />如果di取1，则每次冲突之后，向后移动1个位置.如果di取值可能为1,-1,2,-2,4,-4,9,-9,16,-16,...k*k,-k*k(k&lt;=m/2)&nbsp;<br />称二次探测再散列。如果di取值可能为伪随机数列。称伪随机探测再散列。仍然以学生排号作为例子，<br />现有两名同学，李四，吴用。李四与吴用事先已排好序，现新来一名同学，名字叫王五，对它进行编制<br />
<table border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td width="60">10..</td>
<td width="60">....</td>
<td width="60">22</td>
<td width="60">..</td>
<td width="60">..</td>
<td width="60">25</td></tr>
<tr>
<td style="background-color: #c0c0c0" width="35">李四..</td>
<td style="background-color: #c0c0c0" width="35">....</td>
<td style="background-color: #c0c0c0" width="35">吴用</td>
<td style="background-color: #c0c0c0" width="35">..</td>
<td style="background-color: #c0c0c0" width="35">..</td>
<td style="background-color: #c0c0c0" width="35">25</td></tr></tbody></table>&nbsp;&nbsp;<span style="color: #ff6600">&nbsp;赵刚未来之前<br /></span>
<table border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td width="60">10..</td>
<td width="60">..</td>
<td width="60">22</td>
<td width="60">23</td>
<td width="60">25</td></tr>
<tr>
<td style="background-color: #c0c0c0" width="35">李四..</td>
<td style="background-color: #c0c0c0" width="35"></td>
<td style="background-color: #c0c0c0" width="35">吴用</td>
<td style="background-color: #c0c0c0" width="35">王五</td>
<td style="background-color: #c0c0c0" width="35"></td></tr></tbody></table>&nbsp;&nbsp;<span style="color: #ff6600"> (a)线性探测再散列对赵刚进行编址，且di=1<br /></span>
<table border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td width="60">10...</td>
<td width="60">20</td>
<td width="60">22</td>
<td width="60">..</td>
<td width="60">25</td></tr>
<tr>
<td style="background-color: #c0c0c0" width="35">李四..</td>
<td style="background-color: #c0c0c0" width="35">王五</td>
<td style="background-color: #c0c0c0" width="35">吴用</td>
<td style="background-color: #c0c0c0" width="35"></td>
<td style="background-color: #c0c0c0" width="35"></td></tr></tbody></table>&nbsp;&nbsp; <span style="color: #ff6600">(b)二次探测再散列，且di=-2 <br /></span>
<table border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<td width="60">1...</td>
<td width="60">10...</td>
<td width="60">22</td>
<td width="60">..</td>
<td width="60">25</td></tr>
<tr>
<td style="background-color: #c0c0c0">王五..</td>
<td style="background-color: #c0c0c0">李四..</td>
<td style="background-color: #c0c0c0">吴用</td>
<td style="background-color: #c0c0c0"></td>
<td style="background-color: #c0c0c0"></td></tr></tbody></table><span style="color: #ff6600">&nbsp;&nbsp; (c)伪随机探测再散列,伪随机序列为:5,3,2</span> <br /><br /><strong>b)再哈希法</strong>&nbsp;<br />当发生冲突时，使用第二个、第三个、哈希函数计算地址，直到无冲突时。缺点：计算时间增加。<br />比如上面第一次按照姓首字母进行哈希，如果产生冲突可以按照姓字母首字母第二位进行哈希，再冲突，第三位，直到不冲突为止<br /><strong>c)链地址法<br /></strong>将所有关键字为同义词的记录存储在同一线性链表中。如下：<br /><img border="0" alt="" src="http://images.cnblogs.com/cnblogs_com/jillzhang/class33-01.jpg" width="314" height="294" /><br />因此这种方法，可以近似的认为是筒子里面套筒子<br /><strong>d.建立一个公共溢出区</strong><br />假设哈希函数的值域为[0,m-1],则设向量HashTable[0..m-1]为基本表，另外设立存储空间向量OverTable[0..v]用以存储发生冲突的记录。<br />经过以上方法，基本可以解决掉hash算法冲突的问题。<br /></p></div><img src ="http://www.cppblog.com/wanghaiguang/aggbug/176500.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-05-28 15:54 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/05/28/176500.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>重建二叉树（编程之美）</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/05/24/176012.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Thu, 24 May 2012 05:57:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/05/24/176012.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/176012.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/05/24/176012.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/176012.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/176012.html</trackback:ping><description><![CDATA[<div>本文转自：<a href="http://www.cppblog.com/humanchao/default.html?page=2">http://www.cppblog.com/humanchao/default.html?page=2</a><br /><br /><span style="font-size: 14pt">已知前序和中序：<br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;NODE&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;NODE&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pLeft;<br />&nbsp;&nbsp;&nbsp;&nbsp;NODE&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pRight;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;chValue;<br />};<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;&nbsp;CharInStrFirstPos(</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;ch,&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">str,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nLen)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pOrgStr&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;str;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(nLen&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;ch&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">str)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str</span><span style="color: #000000">++</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nLen</span><span style="color: #000000">--</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;(nLen&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)&nbsp;</span><span style="color: #000000">?</span><span style="color: #000000">&nbsp;(str&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;pOrgStr)&nbsp;:&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;ReBuild_PreIn(</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pPreOrder,&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pInOrder,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nTreeLen,&nbsp;NODE&nbsp;</span><span style="color: #000000">**</span><span style="color: #000000">pRoot)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(pPreOrder&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;pInOrder&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;NODE&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pTemp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;NODE;<br />&nbsp;&nbsp;&nbsp;&nbsp;pTemp</span><span style="color: #000000">-&gt;</span><span style="color: #000000">chValue&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pPreOrder;<br />&nbsp;&nbsp;&nbsp;&nbsp;pTemp</span><span style="color: #000000">-&gt;</span><span style="color: #000000">pLeft&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;NULL;<br />&nbsp;&nbsp;&nbsp;&nbsp;pTemp</span><span style="color: #000000">-&gt;</span><span style="color: #000000">pRight&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;NULL;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">*</span><span style="color: #000000">pRoot&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pRoot&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pTemp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(nTreeLen&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nLeftLen&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;CharInStrFirstPos(</span><span style="color: #000000">*</span><span style="color: #000000">pPreOrder,&nbsp;pInOrder,&nbsp;nTreeLen);<br />&nbsp;&nbsp;&nbsp;&nbsp;assert(nLeftLen&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nRightLen&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;nTreeLen&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;nLeftLen&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(nLeftLen&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReBuild_PreIn(pPreOrder&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;pInOrder,&nbsp;nLeftLen,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">((</span><span style="color: #000000">*</span><span style="color: #000000">pRoot)</span><span style="color: #000000">-&gt;</span><span style="color: #000000">pLeft));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(nRightLen&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReBuild_PreIn(pPreOrder&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;nLeftLen&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;pInOrder&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;nLeftLen&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nRightLen,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">((</span><span style="color: #000000">*</span><span style="color: #000000">pRoot)</span><span style="color: #000000">-&gt;</span><span style="color: #000000">pRight));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span></div><br />已知后序和中序：<br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000"><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;ReBuild_AftIn(</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pAftOrder,&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pInOrder,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nTreeLen,&nbsp;NODE&nbsp;</span><span style="color: #000000">**</span><span style="color: #000000">pRoot)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(pAftOrder&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;pInOrder&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NODE&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pTemp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;NODE;<br />&nbsp;&nbsp;&nbsp;&nbsp;pTemp</span><span style="color: #000000">-&gt;</span><span style="color: #000000">chValue&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pAftOrder;<br />&nbsp;&nbsp;&nbsp;&nbsp;pTemp</span><span style="color: #000000">-&gt;</span><span style="color: #000000">pLeft&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;NULL;<br />&nbsp;&nbsp;&nbsp;&nbsp;pTemp</span><span style="color: #000000">-&gt;</span><span style="color: #000000">pRight&nbsp;&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;NULL;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">*</span><span style="color: #000000">pRoot&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pRoot&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pTemp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(nTreeLen&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nLeftLen&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;CharInStrFirstPos(</span><span style="color: #000000">*</span><span style="color: #000000">pAftOrder,&nbsp;pInOrder,&nbsp;nTreeLen);<br />&nbsp;&nbsp;&nbsp;&nbsp;assert(nLeftLen&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nRightLen&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;nTreeLen&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;nLeftLen&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(nLeftLen&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReBuild_AftIn(pAftOrder&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;nRightLen&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;pInOrder,&nbsp;nLeftLen,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">((</span><span style="color: #000000">*</span><span style="color: #000000">pRoot)</span><span style="color: #000000">-&gt;</span><span style="color: #000000">pLeft));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(nRightLen&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReBuild_AftIn(pAftOrder&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;pInOrder&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;nLeftLen&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nRightLen,&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">((</span><span style="color: #000000">*</span><span style="color: #000000">pRoot)</span><span style="color: #000000">-&gt;</span><span style="color: #000000">pRight));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span></div><br />我上传了一个工VC的工程，有兴<a title="点此下载" href="/Files/wanghaiguang/重建二叉树/BuildTree.rar">点此下载</a>趣的朋友。代码参考于《编程之美》。<br /></span></div><img src ="http://www.cppblog.com/wanghaiguang/aggbug/176012.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-05-24 13:57 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/05/24/176012.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>归并排序</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/05/23/175890.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Wed, 23 May 2012 06:01:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/05/23/175890.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/175890.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/05/23/175890.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/175890.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/175890.html</trackback:ping><description><![CDATA[本文转自：<a href="http://www.cnblogs.com/jillzhang/archive/2007/09/16/894936.html"><font color="#4371a6">http://www.cnblogs.com/jillzhang/archive/2007/09/16/894936.html</font></a><br /><br /><br />
<h1 class="postTitle"><a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/jillzhang/archive/2007/09/16/894936.html"><font color="#4371a6">递归算法学习系列二（归并排序）</font></a> </h1>
<div class="clear"></div>
<div class="postBody">
<div id="cnblogs_post_body">
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 归并排序是利用递归和分而治之的技术将数据序列划分成为越来越小的半子表，再对半子表排序，最后再用递归步骤将排好序的半子表合并成为越来越大的有序序列，归并排序包括两个步骤，分别为：</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1）划分子表</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2）合并半子表&nbsp; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; 首先我们来讨论归并算法，归并算法将一系列数据放到一个向量中，索引范围为[first,last],这个序列由两个排好序的子表构成，以索引终点（mid）为分界线，以下面一个序列为例</p>
<p>&nbsp;&nbsp;&nbsp; 7，10，19，25，12，17，21，30，48</p>
<p>&nbsp;&nbsp;&nbsp;这样的一个序列中，分为两个子序列 7,10,19,25&nbsp; 和 12,17,21,30,48，如下图所示：</p>
<p>&nbsp;&nbsp; <a href="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image.png" atomicselection="true"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_thumb.png" width="922" height="85" /></a>&nbsp;</p>
<p>再使用归并算法的时候的步骤如下：</p>
<p>&nbsp;第一步：比较v[indexA]=7和v[indexB]=12,将较小的v[indexA]取出来放到临时向量tempArray中，然后indexA加1</p>
<p>&nbsp; <a href="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_1.png" atomicselection="true"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_thumb_1.png" width="946" height="91" /></a> </p>
<p>&nbsp;</p>
<p>&nbsp;第二步：比较v[indexA]=10和v[indexB]=12,将较小的10放到临时变量tempArray中，然后indexA++;</p>
<p>&nbsp; <a href="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_2.png" atomicselection="true"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_thumb_2.png" width="856" height="90" /></a> </p>
<p>第三步：比较v[indexA]=19与v[indexB]=12，将较小的12存放到临时变量tempArray中，然后indexB++;</p>
<p>&nbsp;&nbsp; <a href="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_3.png" atomicselection="true"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_thumb_3.png" width="875" height="86" /></a> </p>
<p>第四步到第七步：按照以上规则，进行比对和存储，得到如下结果： </p>
<p>&nbsp;&nbsp; <a href="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_4.png" atomicselection="true"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_thumb_4.png" width="784" height="90" /></a> </p>
<p>最后一步：将子表b中剩余项添加到临时向量tempArray中 </p>
<p>&nbsp;&nbsp; <a href="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_5.png" atomicselection="true"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_thumb_5.png" width="789" height="91" /></a>&nbsp; </p>
<p>然后将临时变量中的值按照索引位置，拷贝回向量v中，就完成了对向量v的归并排序</p>
<p>算法函数为：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Merger(</span><span style="color: #0000ff">int</span><span style="color: #000000">[]&nbsp;v,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;first,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;mid,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;last)<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Queue</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;tempV&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Queue</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;</span><span style="color: #000000">();<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;indexA,&nbsp;indexB;<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">设置indexA，并扫描subArray1&nbsp;[first,mid]<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">设置indexB,并扫描subArray2&nbsp;[mid,last]</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indexA&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;first;<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indexB&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;mid;<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">在没有比较完两个子标的情况下，比较&nbsp;v[indexA]和v[indexB]<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">将其中小的放到临时变量tempV中</span><span style="color: #008000"><br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(indexA&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;mid&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;indexB&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;last)<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(v[indexA]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;v[indexB])<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tempV.Enqueue(v[indexA]);<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indexA</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tempV.Enqueue(v[indexB]);<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indexB</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">复制没有比较完子表中的元素</span><span style="color: #008000"><br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(indexA&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;mid)<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tempV.Enqueue(v[indexA]);<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indexA</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(indexB&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;last)<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tempV.Enqueue(v[indexB]);<br /></span><span style="color: #008080">33</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;indexB</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">34</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">35</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;index&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">36</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(tempV.Count&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)<br /></span><span style="color: #008080">37</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">38</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v[first</span><span style="color: #000000">+</span><span style="color: #000000">index]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;tempV.Dequeue();<br /></span><span style="color: #008080">39</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">40</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">41</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span></div>
<p>&nbsp;&nbsp;&nbsp; 实现归并排序；归并排序算法分为两步，第一步：先将原来的数据表分成排好序的子表，然后调用&nbsp;Merger&nbsp; 对子表进行归并，使之成为有序表，例如有如下向量： 
<p>&nbsp; 25，10，7，19，3，48，12，17，56，30，21</p>
<p>对此序列进行归并排序的步骤为：</p>
<p>&nbsp;&nbsp; <a href="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_6.png" atomicselection="true"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_thumb_6.png" width="983" height="270" /></a></p>
<p>&nbsp;&nbsp;&nbsp; 归并算法函数为</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;MergerSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">[]&nbsp;v,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;first,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;last)<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(first&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;last)<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;mid&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;(first&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;last)&nbsp;</span><span style="color: #000000">/</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MergerSort(v,&nbsp;first,&nbsp;mid);<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MergerSort(v,&nbsp;mid,&nbsp;last);<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Merger(v,&nbsp;first,&nbsp;mid,&nbsp;last);<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span></div>
<p>&nbsp;&nbsp;&nbsp; 归并算法的划分子表和归并子表与原数据序列次序无关，因此算法的最坏情况，最坏情况和平均情况时间复杂度是一样的 
<p>下面是归并算法的函数调用图 
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a></a>&nbsp; <a href="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_7.png" atomicselection="true"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://images.cnblogs.com/cnblogs_com/jillzhang/WindowsLiveWriter/3b1f68344300_F9E8/image_thumb_7.png" width="839" height="326" /></a> <br /><br />示例程序： <a title="MergerSort.rar" href="http://www.cppblog.com/Files/wanghaiguang/归并排序/MergerSort.rar"><font color="#4371a6">MergerSort.rar</font></a></p></div></div><img src ="http://www.cppblog.com/wanghaiguang/aggbug/175890.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-05-23 14:01 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/05/23/175890.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）数据结构和算法Flash动画演示 </title><link>http://www.cppblog.com/wanghaiguang/archive/2012/04/13/171217.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Fri, 13 Apr 2012 04:05:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/04/13/171217.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/171217.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/04/13/171217.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/171217.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/171217.html</trackback:ping><description><![CDATA[<span style="font-size: 14pt">本文转自：<a href="http://www.cppblog.com/humanchao/archive/2008/07/24/57063.html">http://www.cppblog.com/humanchao/archive/2008/07/24/57063.html</a><br /><br />学习数据结构和算法的好东西，非常形象，制作水平比较高，向制作者致敬！里面包含以下算法的过程演示：<br /><br /><span style="color: #9830ff; font-weight: bold">B树的删除</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">B树的生长过程</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">三元组表的转置</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">中序线索化二叉树</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">串的顺序存储</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">二分查找</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">二叉排序树的删除</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">二叉排序树的生成</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">二叉树的建立</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">克鲁斯卡尔算法构造最小生成树</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">冒泡排序</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">分块查找</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">单链表结点的删除</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">单链表结点的插入</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">图的深度优先遍历</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">基数排序</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">堆排序</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">头插法建单链表</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">寻找中序线索化二叉树指定结点的前驱</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">寻找中序线索化二叉树指定结点的后继</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">尾插法建表</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">希儿排序</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">开放定址法建立散列表</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">循环队列操作演示</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">快速排序</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">拉链法创建散列表</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">拓扑排序</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">数据结构和算法Flash动画演示.rar</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">最短路径</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">朴素串匹配算法过程示意</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">构造哈夫曼树的算法模拟</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">构造哈夫曼树过程</span><br style="color: #9830ff; font-weight: bold" /><span style="color: #9830ff; font-weight: bold">栈与递归<br />...更多<br /><br /><a title="点击下载" href="/Files/wanghaiguang/数据结构和算法/数据结构和算法Flash动画演示.rar">点击下载</a></span></span> <img src ="http://www.cppblog.com/wanghaiguang/aggbug/171217.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-04-13 12:05 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/04/13/171217.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>求整数1~N范围和为M的所有组合 </title><link>http://www.cppblog.com/wanghaiguang/archive/2012/04/10/170763.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Tue, 10 Apr 2012 04:37:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/04/10/170763.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/170763.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/04/10/170763.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/170763.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/170763.html</trackback:ping><description><![CDATA[<div>转自：<a href="http://www.cppblog.com/humanchao/archive/2008/08/29/60368.html">http://www.cppblog.com/humanchao/archive/2008/08/29/60368.html</a><br /><br /><span style="font-size: 14pt"><span style="font-weight: bold">问题</span>：<span style="font-style: italic">找出整数1~N范围和为M的所有集合，M&lt;=N且M&gt;1，集合里的数不允许重复。</span><br /><br /><span style="font-weight: bold">解答</span>：这个问题用<span style="font-weight: bold">递归</span>解决最简单，代码如下：<br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MAX_NUM&nbsp;20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">要足够大</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;log[MAX_NUM];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">记录和数</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;index&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">log[]数组的当前指针</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;calc(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;start,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n)<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(n&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;index;&nbsp;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;log[j]);<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;start;&nbsp;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log[index</span><span style="color: #000000">++</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;i;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calc(i&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;n&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;i);<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;index</span><span style="color: #000000">--</span><span style="color: #000000">;<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">}</span></div><br />如果允许重复只需要将上面第18条代码改为：<br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">calc(i,&nbsp;n&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;i);</span></div><br />即可。<br /><br /><span style="font-weight: bold">扩展问题</span>：<span style="font-style: italic">在数组{5,1,7,9,2,10,11,4,13,14}中找到和为28的所有集合，集合中不允许有重复的数。</span><br /><br /><span style="font-weight: bold">解答：第一步要先对数组排序，然后按照上去的思路，对程序略做一些改动。</span><br style="font-weight: bold" />代码如下：<br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MAX_NUM&nbsp;20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">要足够大</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;log[MAX_NUM];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">记录和数</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;index&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">log[]数组的当前指针</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;calc__(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">nArr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">数组,&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int</span><span style="color: #000000">&nbsp;start&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">数组起始元素下标,&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nArrLen&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">数组长度,&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;sum)<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(sum&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)&nbsp;&nbsp;<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;index;&nbsp;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;log[j]);<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;start;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;nArrLen;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log[index</span><span style="color: #000000">++</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;nArr[i];&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calc__(nArr,&nbsp;i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;nArrLen,&nbsp;sum&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;nArr[i]);<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;index</span><span style="color: #000000">--</span><span style="color: #000000">;<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">}</span></div><br /></span></div> <img src ="http://www.cppblog.com/wanghaiguang/aggbug/170763.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-04-10 12:37 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/04/10/170763.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>判断单链表是否存在环，判断两个链表是否相交问题详解</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170596.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Mon, 09 Apr 2012 08:56:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170596.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/170596.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170596.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/170596.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/170596.html</trackback:ping><description><![CDATA[<span style="font-size: 12pt"><span style="font-size: 14pt">转自：<a href="http://www.cppblog.com/humanchao/archive/2008/04/17/47357.html">http://www.cppblog.com/humanchao/archive/2008/04/17/47357.html</a><br /><br /><br />有一个单链表，其中可能有一个环，也就是某个节点的next指向的是链表中在它之前的节点，这样在链表的尾部形成一环。<br /><br />问题：<br /><br />1、如何判断一个链表是不是这类链表？<br />2、如果链表为存在环，如何找到环的入口点？<br /><br />解答：<br /><br />一、判断链表是否存在环，办法为：<br /><br />设置两个指针(fast, slow)，初始值都指向头，slow每次前进一步，fast每次前进二步，如果链表存在环，则fast必定先进入环，而slow后进入环，两个指针必定相遇。(当然，fast先行头到尾部为NULL，则为无环链表)程序如下：<br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">bool&nbsp;IsExitsLoop(slist </span><span style="color: #000000">*</span><span style="color: #000000">head)<br />{<br />&nbsp;&nbsp;&nbsp; slist </span><span style="color: #000000">*</span><span style="color: #000000">slow </span><span style="color: #000000"></span><span style="color: #000000">=</span><span style="color: #000000"> head</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">fast = head;<br /></span><span style="color: #000000"></span><span style="color: #000000"><br /></span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp; while</span><span style="color: #000000">&nbsp;(&nbsp;fast&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;fast</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;)&nbsp;<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; slow&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;slow</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; fast&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;fast</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(&nbsp;slow&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;fast&nbsp;)&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp; }<br /><br /></span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp; return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">!</span><span style="color: #000000">(fast&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;fast</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL);<br />}</span></div><br />二、找到环的入口点<br /><br />当fast若与slow相遇时，slow肯定没有走遍历完链表，而fast已经在环内循环了n圈(1&lt;=n)。假设slow走了s步，则fast走了2s步（fast步数还等于s 加上在环上多转的n圈），设环长为r，则：<br /><br />2s = s + nr<br />s= nr<br /><br />设整个链表长L，入口环与相遇点距离为x，起点到环入口点的距离为a。<br />a + x = nr<br />a + x = (n &#8211; 1)r +r = (n-1)r + L - a<br />a = (n-1)r + (L &#8211; a &#8211; x)<br /><br />(L &#8211; a &#8211; x)为相遇点到环入口点的距离，由此可知，从链表头到环入口点等于(n-1)循环内环+相遇点到环入口点，于是我们从链表头、与相遇点分别设一个指针，每次各走一步，两个指针必定相遇，且相遇第一点为环入口点。程序描述如下：<br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">slist</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;FindLoopPort(slist </span><span style="color: #000000">*</span><span style="color: #000000">head)<br />{<br />&nbsp;&nbsp;&nbsp; slist </span><span style="color: #000000">*</span><span style="color: #000000">slow&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;head,&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">fast&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;head;<br /><br /></span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp; while</span><span style="color: #000000">&nbsp;(&nbsp;fast&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;fast</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;)&nbsp;<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; slow&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;slow</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; fast&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;fast</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(&nbsp;slow&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;fast&nbsp;)&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp; }<br /><br /></span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp; if</span><span style="color: #000000">&nbsp;(fast&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL&nbsp;</span><span style="color: #000000">||</span><span style="color: #000000">&nbsp;fast</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL)<br />&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp; return</span><span style="color: #000000">&nbsp;NULL;<br /><br />&nbsp;&nbsp;&nbsp; slow&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;head;<br /></span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp; while</span><span style="color: #000000">&nbsp;(slow&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;fast)<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; slow&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;slow</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fast&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;fast</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br />&nbsp;&nbsp;&nbsp; }<br /><br /></span><span style="color: #0000ff">&nbsp;&nbsp;&nbsp; return</span><span style="color: #000000">&nbsp;slow;<br />}</span></div><br /><br />扩展问题：<br /><br />判断两个单链表是否相交，如果相交，给出相交的第一个点（两个链表都不存在环）。<br /><br />比较好的方法有两个：<br /><br />一、将其中一个链表首尾相连，检测另外一个链表是否存在环，如果存在，则两个链表相交，而检测出来的依赖环入口即为相交的第一个点。<br /><br />二、如果两个链表相交，那个两个链表从相交点到链表结束都是相同的节点，我们可以先遍历一个链表，直到尾部，再遍历另外一个链表，如果也可以走到同样的结尾点，则两个链表相交。<br /><br />这时我们记下两个链表length，再遍历一次，长链表节点先出发前进(lengthMax-lengthMin)步，之后两个链表同时前进，每次一步，相遇的第一点即为两个链表相交的第一个点。<br /><br /></span></span> <img src ="http://www.cppblog.com/wanghaiguang/aggbug/170596.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-04-09 16:56 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170596.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>单链表逆序输出</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170594.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Mon, 09 Apr 2012 08:41:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170594.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/170594.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170594.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/170594.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/170594.html</trackback:ping><description><![CDATA[<span style="font-size: 12pt"><span style="font-size: 12pt">转自：<a href="http://www.cppblog.com/humanchao/archive/2008/02/29/43446.html"><span style="font-size: 12pt">http://www.cppblog.com/humanchao/archive/2008/02/29/43446.html</span></a><br /><br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;printSList(slist&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pList)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;assert(pList);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(pList&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;string&nbsp;str;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(pList)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;string(</span><span style="color: #000000">*</span><span style="color: #000000">pList)&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;str;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pList&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pList</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;str.c_str());<br />}</span></div><br />递归：<br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;printSList(slist&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pList)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;assert(pList);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(pList&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(pList</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;NULL)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pList);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printSList(pList</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pList);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span></div></span></span><br />分配一个数组，把指针放到数组中，然后for倒着打印 <br />Status display(LinkList &amp;L) <br />{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("\n---------------------------显示单链线性表----------------------\n");&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LinkList p;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int n[100];&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int j=100;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p=L-&gt;next; //打印的时候应该从头结点的下一个结点开始打印,否则会出现乱码&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("\n单链表为:\t");&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(p!=NULL)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(;p!=NULL;--j)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n[j-1]=p-&gt;date; //j-1是因为100要存放头结点的位置&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p=p-&gt;next;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(;j&lt;100;j++)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d",n[j]);&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(p);&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1; <br />}//display&nbsp; <img src ="http://www.cppblog.com/wanghaiguang/aggbug/170594.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-04-09 16:41 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170594.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>将字符串里词顺序倒置 </title><link>http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170578.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Mon, 09 Apr 2012 05:47:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170578.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/170578.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170578.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/170578.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/170578.html</trackback:ping><description><![CDATA[<p style="font-size: 14pt"><span style="font-size: 12pt">转自：</span><a href="http://www.cppblog.com/humanchao/archive/2008/09/12/61708.html"><span style="font-size: 12pt">http://www.cppblog.com/humanchao/archive/2008/09/12/61708.html</span></a><br /><br />将字符串里词顺序倒置，如"Times New Roman"变为"Roman New Times"。以空格为分隔符。<br /><br />解决方案为：先将整个字串倒置，然后依次把倒置后串中的每一个单词倒置。<br /><br />这个问题解答的思路很简单，但是要考虑到很多种的情况，比如字符串的头、尾有多余的空格怎么办，如果字符串中只有空格，还有字符串中间可能会有两个以上并列的空格。<br /><br />程序如下：<br /></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;ReverseStr(</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pStr,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;len)<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;assert(pStr);<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;ch;<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;len</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">&nbsp;;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pStr[i];<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pStr[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pStr[len</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">-</span><span style="color: #000000">i];<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pStr[len</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">-</span><span style="color: #000000">i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;ch;<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;ReverseStrWord(</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">pStr,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;len)<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;assert(pStr);<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(len&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">)<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;倒置整个字符串</span><span style="color: #008000"><br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;ReverseStr(pStr,&nbsp;len);<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;处理头多余的空格</span><span style="color: #008000"><br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(pStr[</span><span style="color: #000000">0</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(pStr[i]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;len)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;整个串都是空格</span><span style="color: #008000"><br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;len)<br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;处理尾多余的空格</span><span style="color: #008000"><br /></span><span style="color: #008080">33</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(pStr[len&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(pStr[len&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;len&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;len</span><span style="color: #000000">--</span><span style="color: #000000">;<br /></span><span style="color: #008080">34</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">35</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;start&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;i;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;len;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">36</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">37</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;最后的end要+1</span><span style="color: #008000"><br /></span><span style="color: #008080">38</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;len</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)<br /></span><span style="color: #008080">39</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">40</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReverseStr(pStr</span><span style="color: #000000">+</span><span style="color: #000000">start,&nbsp;i</span><span style="color: #000000">-</span><span style="color: #000000">start</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">);<br /></span><span style="color: #008080">41</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">42</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">43</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">44</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;倒置一个单词</span><span style="color: #008000"><br /></span><span style="color: #008080">45</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(pStr[i]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">)<br /></span><span style="color: #008080">46</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">47</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReverseStr(pStr</span><span style="color: #000000">+</span><span style="color: #000000">start,&nbsp;i</span><span style="color: #000000">-</span><span style="color: #000000">start);<br /></span><span style="color: #008080">48</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">49</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;处理内部并列的空格</span><span style="color: #008000"><br /></span><span style="color: #008080">50</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(pStr[start]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">)<br /></span><span style="color: #008080">51</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">52</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(pStr[start]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">)&nbsp;{i</span><span style="color: #000000">++</span><span style="color: #000000">;start</span><span style="color: #000000">++</span><span style="color: #000000">;};<br /></span><span style="color: #008080">53</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">54</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">55</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">56</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">57</span>&nbsp;<span style="color: #000000"></span></div>
<p style="font-size: 14pt">&nbsp;</p> <img src ="http://www.cppblog.com/wanghaiguang/aggbug/170578.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-04-09 13:47 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/04/09/170578.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>给定的文本中，查找其中最长的重复子字符串</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/03/30/169505.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Fri, 30 Mar 2012 05:03:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/03/30/169505.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/169505.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/03/30/169505.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/169505.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/169505.html</trackback:ping><description><![CDATA[<div>转自：<a href="http://apps.hi.baidu.com/share/detail/6937479">http://apps.hi.baidu.com/share/detail/6937479</a><br /><br />
<div id="article_content" class="article_content">对于类似从给定的文本中，查找其中最长的重复子字符串的问题，可以采用&#8220;后缀数组&#8221;来高效地完成此任务。后缀数组使用文本本身和n个附加指针（与文本数组相应的指针数组）来表示输入文本中的n个字符的每个子字符串。<br />&nbsp;&nbsp;&nbsp; 首先,如果输入字符串存储在c[0..n-1]中，那么就可以使用类似于下面的代码比较每对子字符串：<br />&nbsp;&nbsp;&nbsp; maxlen = -1<br />&nbsp;&nbsp;&nbsp; for i = [0, n)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for j = (i, n)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (thislen = comlen(&amp;c[i], &amp;c[j])) &gt; maxlen<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxlen = thislen<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxi = i<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxj = j<br />&nbsp;&nbsp;&nbsp; 当作为comlen函数参数的两个字符串长度相等时，该函数便返回这个长度值，从第一个字符开始：<br />&nbsp;&nbsp;&nbsp; int comlen(char *p, char* q)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = 0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while *p &amp;&amp; (*p++ = *q++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i++<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return i<br />&nbsp;&nbsp;&nbsp; 由于该算法查看所有的字符串对，所以它的时间和n的平方成正比。下面便是使用&#8220;后缀数组&#8221;的解决办法。<br />&nbsp;&nbsp;&nbsp; 如果程序至多可以处理MAXN个字符，这些字符被存储在数组c中：<br />&nbsp;&nbsp;&nbsp; #define MAXN 5000000<br />&nbsp;&nbsp;&nbsp; char c[MAXN], *a[MAXN];<br />&nbsp;&nbsp;&nbsp; 在读取输入时，首先初始化a,这样，每个元素就都指向输入字符串中的相应字符：<br />&nbsp;&nbsp;&nbsp; while (ch = getchar()) != EOF<br />&nbsp;&nbsp;a[n] = &amp;c[n];<br />&nbsp;&nbsp;c[n++] = ch;<br />&nbsp;c[n] = 0 //将数组c中的最后一个元素设为空字符，以终止所有字符串。<br />&nbsp;这样，元素a[0]指向整个字符串，下一个元素指向以第二个字符开始的数组的后缀，等等。如若输入字符串为"banana",该数组将表示这些后缀：<br />&nbsp;a[0]:banana<br />&nbsp;a[1]:anana<br />&nbsp;a[2]:nana<br />&nbsp;a[3]:ana<br />&nbsp;a[4]:na<br />&nbsp;a[5]:a<br />&nbsp;由于数组a中的指针分别指向字符串中的每个后缀，所以将数组a命名为"后缀数组"<br />&nbsp;第二，对后缀数组进行快速排序，以将后缀相近的（变位词）子串集中在一起<br />&nbsp;qsort(a, n, sizeof(char*), pstrcmp)后<br />&nbsp;a[0]:a<br />&nbsp;a[1]:ana<br />&nbsp;a[2]:anana<br />&nbsp;a[3]:banana<br />&nbsp;a[4]:na<br />&nbsp;a[5]:nana<br />&nbsp;第三，使用以下comlen函数对数组进行扫描比较邻接元素，以找出最长重复的字符串：<br />&nbsp;for i = [0, n)<br />&nbsp;&nbsp;&nbsp;&nbsp; if comlen(a[i], a[i+1]) &gt; maxlen<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxlen = comlen(a[i], a[i+1])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxi = i<br />&nbsp;printf("%.*s/n", maxlen, a[maxi])<br />&nbsp;由于少了内层循环，只是多了一次排序，因此该算法的运行时间为O(n logn).&nbsp; <br /><br />完整代码如下：
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdio.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">stdlib.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">string</span><span style="color: #000000">.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MAXCHAR&nbsp;5000&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">最长处理5000个字符</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;c[MAXCHAR],&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">a[MAXCHAR];<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;comlen(&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p,&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">q&nbsp;)<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">*</span><span style="color: #000000">p</span><span style="color: #000000">++</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">q</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;)<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i;<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;i;<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;pstrcmp(&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p1,&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p2&nbsp;)<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;strcmp(&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">(</span><span style="color: #0000ff">char</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">)p1,&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">(</span><span style="color: #0000ff">char</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">const</span><span style="color: #000000">*</span><span style="color: #000000">)p2&nbsp;);<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main(&nbsp;)<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;ch;<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,&nbsp;temp;<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;maxlen</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;maxi</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">Please&nbsp;input&nbsp;your&nbsp;string:\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(&nbsp;(ch</span><span style="color: #000000">=</span><span style="color: #000000">getchar())</span><span style="color: #000000">!=</span><span style="color: #000000">'</span><span style="color: #000000">\n</span><span style="color: #000000">'</span><span style="color: #000000">&nbsp;)<br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[n]</span><span style="color: #000000">=&amp;</span><span style="color: #000000">c[n];<br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[n</span><span style="color: #000000">++</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">ch;<br /></span><span style="color: #008080">33</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">34</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;c[n]</span><span style="color: #000000">=</span><span style="color: #000000">'</span><span style="color: #000000">\0</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">35</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;qsort(&nbsp;a,&nbsp;n,&nbsp;</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(</span><span style="color: #0000ff">char</span><span style="color: #000000">*</span><span style="color: #000000">),&nbsp;pstrcmp&nbsp;);<br /></span><span style="color: #008080">36</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i&nbsp;)<br /></span><span style="color: #008080">37</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">38</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp</span><span style="color: #000000">=</span><span style="color: #000000">comlen(&nbsp;a[i],&nbsp;a[i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;);<br /></span><span style="color: #008080">39</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(&nbsp;temp</span><span style="color: #000000">&gt;</span><span style="color: #000000">maxlen&nbsp;)<br /></span><span style="color: #008080">40</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">41</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxlen</span><span style="color: #000000">=</span><span style="color: #000000">temp;<br /></span><span style="color: #008080">42</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxi</span><span style="color: #000000">=</span><span style="color: #000000">i;<br /></span><span style="color: #008080">43</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">44</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">45</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%.*s\n</span><span style="color: #000000">"</span><span style="color: #000000">,maxlen,&nbsp;a[maxi]);<br /></span><span style="color: #008080">46</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="color: #000000">"</span><span style="color: #000000">PAUSE</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">47</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">48</span>&nbsp;<span style="color: #000000">}</span></div><br /></div></div> <img src ="http://www.cppblog.com/wanghaiguang/aggbug/169505.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-03-30 13:03 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/03/30/169505.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>快速排序（编程珠玑）</title><link>http://www.cppblog.com/wanghaiguang/archive/2012/03/30/169503.html</link><dc:creator>王海光</dc:creator><author>王海光</author><pubDate>Fri, 30 Mar 2012 04:47:00 GMT</pubDate><guid>http://www.cppblog.com/wanghaiguang/archive/2012/03/30/169503.html</guid><wfw:comment>http://www.cppblog.com/wanghaiguang/comments/169503.html</wfw:comment><comments>http://www.cppblog.com/wanghaiguang/archive/2012/03/30/169503.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghaiguang/comments/commentRss/169503.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghaiguang/services/trackbacks/169503.html</trackback:ping><description><![CDATA[<p><span style="font-family: Arial">编程珠玑第二版快速排序<br /></span></p>
<p><span style="font-family: Arial"></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #000000">qsort4(</span><span style="color: #000000">0</span><span style="color: #000000">,&nbsp;n</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">isort3();<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;qsort4(l,&nbsp;u)<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">{&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(u&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;l&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;cutoff)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">cutoff是一个小整数，如果要排序的数组很小，可以用插入排序，速度更快</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swap(L,&nbsp;randint(l,&nbsp;u))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">将第一个数据与后面的数据随即交换，避免要排序的数据已是升序排列或者第一个数据很小</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;x[l];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">t为中间元素，所有数据与其进行比较，小于t的放到左边，大于t的放到右边</span><span style="color: #008000"><br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;l;<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;u</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">do</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;u&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;x[i]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;t);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">从第二个数据开始比较，遇到大于t的数据终止循环</span><span style="color: #008000"><br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000"><br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">do</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="color: #000000">--</span><span style="color: #000000">;<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(x[j]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;t);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">从最后一个数据进行比较，遇到小于t的终止循环</span><span style="color: #008000"><br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000"><br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;j)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">如果i&gt;j，数据分组成功，跳出循环</span><span style="color: #008000"><br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;x[i];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">交换上面两个循环终止时的数据</span><span style="color: #008000"><br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;x[j];<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;temp;<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;swap(l,&nbsp;j);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">交换x[i]与x[j]的值，分组完成</span><span style="color: #008000"><br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;qsort4(l,&nbsp;j</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">排序小于t的数据</span><span style="color: #008000"><br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;qsort4(j</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;u);&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">排序大于t的数据&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">}<br /></span><span style="color: #008080">33</span>&nbsp;<span style="color: #000000"><br /></span><span style="color: #008080">34</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;isort3()&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">插入排序，当排序的数据很少时可以用此排序</span><span style="color: #008000"><br /></span><span style="color: #008080">35</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">{<br /></span><span style="color: #008080">36</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j;<br /></span><span style="color: #008080">37</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;[l,&nbsp;n)<br /></span><span style="color: #008080">38</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;x[i];<br /></span><span style="color: #008080">39</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j</span><span style="color: #000000">=</span><span style="color: #000000">i;&nbsp;j</span><span style="color: #000000">&gt;</span><span style="color: #000000">0</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;x[j</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">&gt;</span><span style="color: #000000">t;&nbsp;j</span><span style="color: #000000">--</span><span style="color: #000000">)<br /></span><span style="color: #008080">40</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">41</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;x[j</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">];<br /></span><span style="color: #008080">42</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">43</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;t;<br /></span><span style="color: #008080">44</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;}</span></div>
<p><br />转自：<a href="http://www.cppblog.com/humanchao/archive/2008/08/18/59241.html">http://www.cppblog.com/humanchao/archive/2008/08/18/59241.html</a><br /></p>
<div class="postTitle"><a id="_213af26b0bf1_HomePageDays_ctl00_DayList_ctl04_TitleUrl" class="postTitle2" href="http://www.cppblog.com/humanchao/archive/2008/08/18/59241.html">Quick Sort C Code Implement</a> </div>
<p><span style="font-size: 14pt"></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;QuickSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;pData,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;left,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;right)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;left,&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;right;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;middle&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pData[(left</span><span style="color: #000000">+</span><span style="color: #000000">right)</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;midlle&nbsp;value</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;iTemp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">do</span><span style="color: #000000"><br />&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(pData[i]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;middle&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;right)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(pData[j]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;middle&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;left)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="color: #000000">--</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;j)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;swap</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iTemp&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pData[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pData[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pData[j];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pData[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;iTemp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="color: #000000">--</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;j)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="color: #000000">--</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;j);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(left&nbsp;&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;j)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QuickSort(pData,left,j);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(right&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;i)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QuickSort(pData,i,right);<br />}<br /></span></div>
<p></span></span>&nbsp;</p>
<h3 class="headline-2 bk-sidecatalog-title"><span style="font-family: Arial" class="headline-content"><span class="headline-content">
<h4>堆排序实现原理</h4>
<p>转自：<a href="http://www.nowamagic.net/algorithm/algorithm_HeapSortStudy.php">http://www.nowamagic.net/algorithm/algorithm_HeapSortStudy.php</a></p>
<p align="center"><img alt="" src="http://www.nowamagic.net/algorithm/images/heapSort_1.jpg" width="641" height="485" /></p>
<p align="center"><img alt="" src="http://www.nowamagic.net/algorithm/images/heapSort_2.jpg" width="641" height="477" /></p>
<p align="center"><img alt="" src="http://www.nowamagic.net/algorithm/images/heapSort_3.jpg" width="700" /></p><br />堆排序</span>C语言描述</span></h3><span style="font-family: Arial; color: #008000">　　
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #008000">//</span><span style="color: #008000">&nbsp;array是待调整的堆数组，i是待调整的数组元素的位置，nlength是数组的长度&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;HeapAdjust(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;array[],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nLength)&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">本函数功能是：根据数组array构建大根堆&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　{&nbsp;<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nChild;&nbsp;<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nTemp;&nbsp;<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(nTemp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[i];&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;nLength;&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;nChild)&nbsp;<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;子结点的位置=2*（父结点位置）+&nbsp;1&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nChild&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;得到子结点中较大的结点&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(nChild&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;nLength&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;array[nChild&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;array[nChild])&nbsp;<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">nChild;&nbsp;<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;如果较大的子结点大于父结点那么把较大的子结点往上移动，替换它的父结点&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(nTemp&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;array[nChild])&nbsp;<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　array[i]</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[nChild];&nbsp;<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　}&nbsp;<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">　</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;否则退出循环&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;&nbsp;<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;最后把需要调整的元素值放到合适的位置&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[nChild]</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;nTemp;&nbsp;<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　}&nbsp;<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">　　}&nbsp;<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">　　</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;堆排序算法&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;HeapSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;array[],</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;length)&nbsp;<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000">　　{&nbsp;<br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;调整序列的前半部分元素，调整完之后第一个元素是序列的最大的元素&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;length&nbsp;</span><span style="color: #000000">/</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&gt;=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">i)&nbsp;<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HeapAdjust(array,i,length);&nbsp;<br /></span><span style="color: #008080">33</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　}&nbsp;<br /></span><span style="color: #008080">34</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;从最后一个元素开始对序列进行调整，不断的缩小调整的范围直到第一个元素&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">35</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;length&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">i)&nbsp;<br /></span><span style="color: #008080">36</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;<br /></span><span style="color: #008080">37</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;把第一个元素和当前的最后一个元素交换，&nbsp;<br /></span><span style="color: #008080">38</span>&nbsp;<span style="color: #008000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;保证当前的最后一个位置的元素都是在现在的这个序列之中最大的&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">39</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　Swap(</span><span style="color: #000000">&amp;</span><span style="color: #000000">array[</span><span style="color: #000000">0</span><span style="color: #000000">],</span><span style="color: #000000">&amp;</span><span style="color: #000000">array[i]);&nbsp;<br /></span><span style="color: #008080">40</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;不断缩小调整heap的范围，每一次调整完毕保证第一个元素是当前序列的最大值&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">41</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HeapAdjust(array,</span><span style="color: #000000">0</span><span style="color: #000000">,i);&nbsp;<br /></span><span style="color: #008080">42</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br /></span><span style="color: #008080">43</span>&nbsp;<span style="color: #000000">　　}&nbsp;</span></div></span>
<div class="spctrl"></div><strong>堆排序算法（C++描述） <br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #0000ff">#define</span><span style="color: #000000">&nbsp;MAX&nbsp;100</span><span style="color: #008000">//</span><span style="color: #008000">数据元素的最大个数&nbsp;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">　　typedef&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">　　{&nbsp;<br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;r[MAX];&nbsp;<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;length;&nbsp;<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000">　　}SqList;</span><span style="color: #008000">//</span><span style="color: #008000">定义一个线性表用于存放数据元素&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;HeapAdjust(SqList&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">L,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;s,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m)&nbsp;<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000">　　{<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">已知L.r[s<img src="http://www.cppblog.com/Images/dot.gif"  alt="" />m]中记录除L.r[s]外均满足堆的定义，本函数用于使L.r[s<img src="http://www.cppblog.com/Images/dot.gif"  alt="" />m]成为一个大顶堆&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j;&nbsp;<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;e</span><span style="color: #000000">=</span><span style="color: #000000">L.r[s];&nbsp;<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　</span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">2</span><span style="color: #000000">*</span><span style="color: #000000">s;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">m;j</span><span style="color: #000000">*=</span><span style="color: #000000">2</span><span style="color: #000000">)&nbsp;<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　{&nbsp;<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　</span><span style="color: #0000ff">if</span><span style="color: #000000">(j</span><span style="color: #000000">&lt;</span><span style="color: #000000">M</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">L.R[J]</span><span style="color: #000000">&lt;</span><span style="color: #000000">L.R[J</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">])&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j;&nbsp;<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(e</span><span style="color: #000000">&gt;=</span><span style="color: #000000">L.r[j])&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;&nbsp;<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L.r[s]</span><span style="color: #000000">=</span><span style="color: #000000">L.r[j];&nbsp;<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　s</span><span style="color: #000000">=</span><span style="color: #000000">j;&nbsp;<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　}&nbsp;<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L.r[s]</span><span style="color: #000000">=</span><span style="color: #000000">e;&nbsp;<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">　　}&nbsp;<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">　　</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;HeapSort(SqList&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">L)&nbsp;<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">　　{<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">对顺序表L进行堆排序&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,e;&nbsp;<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">L.length</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">;i</span><span style="color: #000000">&gt;</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">--</span><span style="color: #000000">)&nbsp;<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HeapAdjust(L,i,L.length);&nbsp;<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">L.length;i</span><span style="color: #000000">&gt;</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">--</span><span style="color: #000000">)&nbsp;<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　{<br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">将大顶堆的顶记录和最后一个记录相互交换&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #008000"></span><span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　e</span><span style="color: #000000">=</span><span style="color: #000000">L.r[</span><span style="color: #000000">1</span><span style="color: #000000">];&nbsp;<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　L.r[</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">L.r[i];&nbsp;<br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L.r[i]</span><span style="color: #000000">=</span><span style="color: #000000">e;&nbsp;<br /></span><span style="color: #008080">33</span>&nbsp;<span style="color: #000000">　　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HeapAdjust(L,</span><span style="color: #000000">1</span><span style="color: #000000">,i</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);&nbsp;<br /></span><span style="color: #008080">34</span>&nbsp;<span style="color: #000000">　&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;　}&nbsp;<br /></span><span style="color: #008080">35</span>&nbsp;<span style="color: #000000">　　}&nbsp;</span></div></strong> <img src ="http://www.cppblog.com/wanghaiguang/aggbug/169503.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghaiguang/" target="_blank">王海光</a> 2012-03-30 12:47 <a href="http://www.cppblog.com/wanghaiguang/archive/2012/03/30/169503.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>