﻿<?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++博客-zhuxin-文章分类-排序算法</title><link>http://www.cppblog.com/zhuxin/category/20125.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 02 Nov 2012 08:51:59 GMT</lastBuildDate><pubDate>Fri, 02 Nov 2012 08:51:59 GMT</pubDate><ttl>60</ttl><item><title>快速排序</title><link>http://www.cppblog.com/zhuxin/articles/193700.html</link><dc:creator>zhuxin</dc:creator><author>zhuxin</author><pubDate>Mon, 22 Oct 2012 15:47:00 GMT</pubDate><guid>http://www.cppblog.com/zhuxin/articles/193700.html</guid><wfw:comment>http://www.cppblog.com/zhuxin/comments/193700.html</wfw:comment><comments>http://www.cppblog.com/zhuxin/articles/193700.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhuxin/comments/commentRss/193700.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhuxin/services/trackbacks/193700.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;快速排序的平均情况下是O(nlogn)，但是一般都比其他运行时间为O(nlogn)的算法都要快，因为它隐藏的常数因子比较小，但是在最坏情况之下，快速排序的运行时间是O(n<sup><font size="2">2</font></sup>)。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>性能分析<br /></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red">最好情况</span>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;每次基准的最终位置都是在数组中间位置，从而使规模为N的问题分为2个规模为N/2的问题，即T(n) = 2T(n/2) + &#920;(n)，用递归树分析或者主定理得到时间T(n) = O(nlogn)。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red">&nbsp;最坏情况</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;每次基准的最终位置都是第一个位置，从而规模为N的问题分为一个规模为N-1的问题，即T(n) = T(n-1) + &#920;(n),用递归树分析可得运行时间T(n) = O(n<sup><font size="2">2</font></sup>)。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red">平均情况</span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;假设规模为N的问题分为一个规模为9/10N的问题和规模为1/10N的问题，即T(n) = T(9n/10) + T(n/10) + &#920;(n),用递归树分析可得T(n) = O(nlogn)，而且比分区9:1要更平均（也就是情况更好）的概率为80%，所以在绝大部分情况下快速排序算法的运行时间为O(nlogn)。</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><img id="Codehighlighter1_0_62_Open_Image" onclick="this.style.display='none'; Codehighlighter1_0_62_Open_Text.style.display='none'; Codehighlighter1_0_62_Closed_Image.style.display='inline'; Codehighlighter1_0_62_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_0_62_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_0_62_Closed_Text.style.display='none'; Codehighlighter1_0_62_Open_Image.style.display='inline'; Codehighlighter1_0_62_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_0_62_Closed_Text">/**/</span><span id="Codehighlighter1_0_62_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;名称：快速排序<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;最坏时间复杂度：O(n&nbsp;*&nbsp;n)<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;最好时间复杂度：O(n&nbsp;*&nbsp;lg(n))<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" /></span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">time.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></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">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><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;</span><span style="color: #0000ff">int</span><span style="color: #000000">);<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><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;</span><span style="color: #0000ff">int</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">);<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;partition(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">);<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></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">14</span><span style="color: #000000"><img id="Codehighlighter1_250_902_Open_Image" onclick="this.style.display='none'; Codehighlighter1_250_902_Open_Text.style.display='none'; Codehighlighter1_250_902_Closed_Image.style.display='inline'; Codehighlighter1_250_902_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_250_902_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_250_902_Closed_Text.style.display='none'; Codehighlighter1_250_902_Open_Image.style.display='inline'; Codehighlighter1_250_902_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_250_902_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_250_902_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n;<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">array;<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img id="Codehighlighter1_316_345_Open_Image" onclick="this.style.display='none'; Codehighlighter1_316_345_Open_Text.style.display='none'; Codehighlighter1_316_345_Closed_Image.style.display='inline'; Codehighlighter1_316_345_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_316_345_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_316_345_Closed_Text.style.display='none'; Codehighlighter1_316_345_Open_Image.style.display='inline'; Codehighlighter1_316_345_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;srand(time(NULL));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_316_345_Closed_Text">/**/</span><span id="Codehighlighter1_316_345_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">void&nbsp;srand(unsigned&nbsp;seed)；</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">18</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">true</span><span style="color: #000000">)<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img id="Codehighlighter1_368_886_Open_Image" onclick="this.style.display='none'; Codehighlighter1_368_886_Open_Text.style.display='none'; Codehighlighter1_368_886_Closed_Image.style.display='inline'; Codehighlighter1_368_886_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_368_886_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_368_886_Closed_Text.style.display='none'; Codehighlighter1_368_886_Open_Image.style.display='inline'; Codehighlighter1_368_886_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_368_886_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_368_886_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&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">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array&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">[n];<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img id="Codehighlighter1_424_433_Open_Image" onclick="this.style.display='none'; Codehighlighter1_424_433_Open_Text.style.display='none'; Codehighlighter1_424_433_Closed_Image.style.display='inline'; Codehighlighter1_424_433_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_424_433_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_424_433_Closed_Text.style.display='none'; Codehighlighter1_424_433_Open_Image.style.display='inline'; Codehighlighter1_424_433_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_424_433_Closed_Text">/**/</span><span id="Codehighlighter1_424_433_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">随机生成数组</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">23</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img id="Codehighlighter1_479_546_Open_Image" onclick="this.style.display='none'; Codehighlighter1_479_546_Open_Text.style.display='none'; Codehighlighter1_479_546_Closed_Image.style.display='inline'; Codehighlighter1_479_546_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_479_546_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_479_546_Closed_Text.style.display='none'; Codehighlighter1_479_546_Open_Image.style.display='inline'; Codehighlighter1_479_546_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_479_546_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_479_546_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img id="Codehighlighter1_518_536_Open_Image" onclick="this.style.display='none'; Codehighlighter1_518_536_Open_Text.style.display='none'; Codehighlighter1_518_536_Closed_Image.style.display='inline'; Codehighlighter1_518_536_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_518_536_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_518_536_Closed_Text.style.display='none'; Codehighlighter1_518_536_Open_Image.style.display='inline'; Codehighlighter1_518_536_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;rand()&nbsp;</span><span style="color: #000000">%</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_518_536_Closed_Text">/**/</span><span id="Codehighlighter1_518_536_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">int&nbsp;rand(void)；</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">26</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">27</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">28</span><span style="color: #000000"><img id="Codehighlighter1_557_563_Open_Image" onclick="this.style.display='none'; Codehighlighter1_557_563_Open_Text.style.display='none'; Codehighlighter1_557_563_Closed_Image.style.display='inline'; Codehighlighter1_557_563_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_557_563_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_557_563_Closed_Text.style.display='none'; Codehighlighter1_557_563_Open_Image.style.display='inline'; Codehighlighter1_557_563_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_557_563_Closed_Text">/**/</span><span id="Codehighlighter1_557_563_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">排序前</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img id="Codehighlighter1_609_656_Open_Image" onclick="this.style.display='none'; Codehighlighter1_609_656_Open_Text.style.display='none'; Codehighlighter1_609_656_Closed_Image.style.display='inline'; Codehighlighter1_609_656_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_609_656_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_609_656_Closed_Text.style.display='none'; Codehighlighter1_609_656_Open_Image.style.display='inline'; Codehighlighter1_609_656_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_609_656_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_609_656_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">31</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;array[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;</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">32</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">33</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">34</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">35</span><span style="color: #000000"><img id="Codehighlighter1_689_696_Open_Image" onclick="this.style.display='none'; Codehighlighter1_689_696_Open_Text.style.display='none'; Codehighlighter1_689_696_Closed_Image.style.display='inline'; Codehighlighter1_689_696_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_689_696_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_689_696_Closed_Text.style.display='none'; Codehighlighter1_689_696_Open_Image.style.display='inline'; Codehighlighter1_689_696_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_689_696_Closed_Text">/**/</span><span id="Codehighlighter1_689_696_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">快速排序</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">36</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quickSort(array,&nbsp;n);<br /></span><span style="color: #008080">37</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">38</span><span style="color: #000000"><img id="Codehighlighter1_736_742_Open_Image" onclick="this.style.display='none'; Codehighlighter1_736_742_Open_Text.style.display='none'; Codehighlighter1_736_742_Closed_Image.style.display='inline'; Codehighlighter1_736_742_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_736_742_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_736_742_Closed_Text.style.display='none'; Codehighlighter1_736_742_Open_Image.style.display='inline'; Codehighlighter1_736_742_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_736_742_Closed_Text">/**/</span><span id="Codehighlighter1_736_742_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">排序后</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">39</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">40</span><span style="color: #000000"><img id="Codehighlighter1_788_835_Open_Image" onclick="this.style.display='none'; Codehighlighter1_788_835_Open_Text.style.display='none'; Codehighlighter1_788_835_Closed_Image.style.display='inline'; Codehighlighter1_788_835_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_788_835_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_788_835_Closed_Text.style.display='none'; Codehighlighter1_788_835_Open_Image.style.display='inline'; Codehighlighter1_788_835_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_788_835_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_788_835_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">41</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;array[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;</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">42</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">43</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">44</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">45</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;array;<br /></span><span style="color: #008080">46</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">47</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">49</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><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;array,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;length)<br /></span><span style="color: #008080">50</span><span style="color: #000000"><img id="Codehighlighter1_943_984_Open_Image" onclick="this.style.display='none'; Codehighlighter1_943_984_Open_Text.style.display='none'; Codehighlighter1_943_984_Closed_Image.style.display='inline'; Codehighlighter1_943_984_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_943_984_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_943_984_Closed_Text.style.display='none'; Codehighlighter1_943_984_Open_Image.style.display='inline'; Codehighlighter1_943_984_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_943_984_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_943_984_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">51</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;__quickSort(array,&nbsp;</span><span style="color: #000000">0</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">);<br /></span><span style="color: #008080">52</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">53</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><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;array,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;l,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;r)<br /></span><span style="color: #008080">54</span><span style="color: #000000"><img id="Codehighlighter1_1029_1186_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1029_1186_Open_Text.style.display='none'; Codehighlighter1_1029_1186_Closed_Image.style.display='inline'; Codehighlighter1_1029_1186_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1029_1186_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1029_1186_Closed_Text.style.display='none'; Codehighlighter1_1029_1186_Open_Image.style.display='inline'; Codehighlighter1_1029_1186_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1029_1186_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1029_1186_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">55</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(l&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;r)<br /></span><span style="color: #008080">56</span><span style="color: #000000"><img id="Codehighlighter1_1050_1184_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1050_1184_Open_Text.style.display='none'; Codehighlighter1_1050_1184_Closed_Image.style.display='inline'; Codehighlighter1_1050_1184_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1050_1184_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1050_1184_Closed_Text.style.display='none'; Codehighlighter1_1050_1184_Open_Image.style.display='inline'; Codehighlighter1_1050_1184_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1050_1184_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1050_1184_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">57</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;pivot&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;partition(array,&nbsp;l,&nbsp;r);<br /></span><span style="color: #008080">58</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__quickSort(array,&nbsp;l,&nbsp;pivot&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">59</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__quickSort(array,&nbsp;pivot&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;r);<br /></span><span style="color: #008080">60</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">61</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">62</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;partition(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;array,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;l,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;r)<br /></span><span style="color: #008080">63</span><span style="color: #000000"><img id="Codehighlighter1_1228_1586_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1228_1586_Open_Text.style.display='none'; Codehighlighter1_1228_1586_Closed_Image.style.display='inline'; Codehighlighter1_1228_1586_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1228_1586_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1228_1586_Closed_Text.style.display='none'; Codehighlighter1_1228_1586_Open_Image.style.display='inline'; Codehighlighter1_1228_1586_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1228_1586_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1228_1586_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">64</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;pivot&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[r];<br /></span><span style="color: #008080">65</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;l&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">66</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j;<br /></span><span style="color: #008080">67</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;l;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;r;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /></span><span style="color: #008080">68</span><span style="color: #000000"><img id="Codehighlighter1_1318_1487_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1318_1487_Open_Text.style.display='none'; Codehighlighter1_1318_1487_Closed_Image.style.display='inline'; Codehighlighter1_1318_1487_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1318_1487_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1318_1487_Closed_Text.style.display='none'; Codehighlighter1_1318_1487_Open_Image.style.display='inline'; Codehighlighter1_1318_1487_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1318_1487_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1318_1487_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">69</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(array[j]&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;pivot)<br /></span><span style="color: #008080">70</span><span style="color: #000000"><img id="Codehighlighter1_1359_1481_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1359_1481_Open_Text.style.display='none'; Codehighlighter1_1359_1481_Closed_Image.style.display='inline'; Codehighlighter1_1359_1481_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1359_1481_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1359_1481_Closed_Text.style.display='none'; Codehighlighter1_1359_1481_Open_Image.style.display='inline'; Codehighlighter1_1359_1481_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1359_1481_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1359_1481_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">71</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&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">72</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[i];<br /></span><span style="color: #008080">73</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[j];<br /></span><span style="color: #008080">74</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;temp;<br /></span><span style="color: #008080">75</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">76</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">77</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[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">78</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;array[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">=</span><span style="color: #000000">&nbsp;array[r];<br /></span><span style="color: #008080">79</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;array[r]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;temp;<br /></span><span style="color: #008080">80</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</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">81</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">82</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>
<p><br /></p><img src ="http://www.cppblog.com/zhuxin/aggbug/193700.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhuxin/" target="_blank">zhuxin</a> 2012-10-22 23:47 <a href="http://www.cppblog.com/zhuxin/articles/193700.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>冒泡排序</title><link>http://www.cppblog.com/zhuxin/articles/193694.html</link><dc:creator>zhuxin</dc:creator><author>zhuxin</author><pubDate>Mon, 22 Oct 2012 13:50:00 GMT</pubDate><guid>http://www.cppblog.com/zhuxin/articles/193694.html</guid><wfw:comment>http://www.cppblog.com/zhuxin/comments/193694.html</wfw:comment><comments>http://www.cppblog.com/zhuxin/articles/193694.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhuxin/comments/commentRss/193694.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhuxin/services/trackbacks/193694.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;冒泡排序算法是一种流行的排序算法，它重复的交换相邻的两个反序元素。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BUBBLESORT（A）<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i &lt;- 1 to length(A)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do for j &lt;- length[A] downto i + 1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do if A[j] &gt; A[j - 1]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;then exchange A[j] &lt;-&gt; A[j - 1]
<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><img id="Codehighlighter1_0_35_Open_Image" onclick="this.style.display='none'; Codehighlighter1_0_35_Open_Text.style.display='none'; Codehighlighter1_0_35_Closed_Image.style.display='inline'; Codehighlighter1_0_35_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_0_35_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_0_35_Closed_Text.style.display='none'; Codehighlighter1_0_35_Open_Image.style.display='inline'; Codehighlighter1_0_35_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_0_35_Closed_Text">/**/</span><span id="Codehighlighter1_0_35_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;名称：冒泡排序<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;时间复杂度：O(n&nbsp;*&nbsp;n)<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" /></span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">time.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></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;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;bubbleSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">);<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></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">11</span><span style="color: #000000"><img id="Codehighlighter1_159_812_Open_Image" onclick="this.style.display='none'; Codehighlighter1_159_812_Open_Text.style.display='none'; Codehighlighter1_159_812_Closed_Image.style.display='inline'; Codehighlighter1_159_812_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_159_812_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_159_812_Closed_Text.style.display='none'; Codehighlighter1_159_812_Open_Image.style.display='inline'; Codehighlighter1_159_812_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_159_812_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_159_812_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n;<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">array;<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img id="Codehighlighter1_225_254_Open_Image" onclick="this.style.display='none'; Codehighlighter1_225_254_Open_Text.style.display='none'; Codehighlighter1_225_254_Closed_Image.style.display='inline'; Codehighlighter1_225_254_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_225_254_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_225_254_Closed_Text.style.display='none'; Codehighlighter1_225_254_Open_Image.style.display='inline'; Codehighlighter1_225_254_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;srand(time(NULL));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_225_254_Closed_Text">/**/</span><span id="Codehighlighter1_225_254_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">void&nbsp;srand(unsigned&nbsp;seed)；</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">true</span><span style="color: #000000">)<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img id="Codehighlighter1_277_796_Open_Image" onclick="this.style.display='none'; Codehighlighter1_277_796_Open_Text.style.display='none'; Codehighlighter1_277_796_Closed_Image.style.display='inline'; Codehighlighter1_277_796_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_277_796_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_277_796_Closed_Text.style.display='none'; Codehighlighter1_277_796_Open_Image.style.display='inline'; Codehighlighter1_277_796_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_277_796_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_277_796_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&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">18</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array&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">[n];<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img id="Codehighlighter1_333_342_Open_Image" onclick="this.style.display='none'; Codehighlighter1_333_342_Open_Text.style.display='none'; Codehighlighter1_333_342_Closed_Image.style.display='inline'; Codehighlighter1_333_342_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_333_342_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_333_342_Closed_Text.style.display='none'; Codehighlighter1_333_342_Open_Image.style.display='inline'; Codehighlighter1_333_342_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_333_342_Closed_Text">/**/</span><span id="Codehighlighter1_333_342_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">随机生成数组</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">20</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img id="Codehighlighter1_388_455_Open_Image" onclick="this.style.display='none'; Codehighlighter1_388_455_Open_Text.style.display='none'; Codehighlighter1_388_455_Closed_Image.style.display='inline'; Codehighlighter1_388_455_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_388_455_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_388_455_Closed_Text.style.display='none'; Codehighlighter1_388_455_Open_Image.style.display='inline'; Codehighlighter1_388_455_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_388_455_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_388_455_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img id="Codehighlighter1_427_445_Open_Image" onclick="this.style.display='none'; Codehighlighter1_427_445_Open_Text.style.display='none'; Codehighlighter1_427_445_Closed_Image.style.display='inline'; Codehighlighter1_427_445_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_427_445_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_427_445_Closed_Text.style.display='none'; Codehighlighter1_427_445_Open_Image.style.display='inline'; Codehighlighter1_427_445_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;rand()&nbsp;</span><span style="color: #000000">%</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_427_445_Closed_Text">/**/</span><span id="Codehighlighter1_427_445_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">int&nbsp;rand(void)；</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">23</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">25</span><span style="color: #000000"><img id="Codehighlighter1_466_472_Open_Image" onclick="this.style.display='none'; Codehighlighter1_466_472_Open_Text.style.display='none'; Codehighlighter1_466_472_Closed_Image.style.display='inline'; Codehighlighter1_466_472_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_466_472_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_466_472_Closed_Text.style.display='none'; Codehighlighter1_466_472_Open_Image.style.display='inline'; Codehighlighter1_466_472_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_466_472_Closed_Text">/**/</span><span id="Codehighlighter1_466_472_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">排序前</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">26</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img id="Codehighlighter1_518_565_Open_Image" onclick="this.style.display='none'; Codehighlighter1_518_565_Open_Text.style.display='none'; Codehighlighter1_518_565_Closed_Image.style.display='inline'; Codehighlighter1_518_565_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_518_565_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_518_565_Closed_Text.style.display='none'; Codehighlighter1_518_565_Open_Image.style.display='inline'; Codehighlighter1_518_565_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_518_565_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_518_565_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;array[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;</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">30</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">31</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">32</span><span style="color: #000000"><img id="Codehighlighter1_598_605_Open_Image" onclick="this.style.display='none'; Codehighlighter1_598_605_Open_Text.style.display='none'; Codehighlighter1_598_605_Closed_Image.style.display='inline'; Codehighlighter1_598_605_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_598_605_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_598_605_Closed_Text.style.display='none'; Codehighlighter1_598_605_Open_Image.style.display='inline'; Codehighlighter1_598_605_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_598_605_Closed_Text">/**/</span><span id="Codehighlighter1_598_605_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">冒泡排序</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">33</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bubbleSort(array,&nbsp;n);<br /></span><span style="color: #008080">34</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">35</span><span style="color: #000000"><img id="Codehighlighter1_646_652_Open_Image" onclick="this.style.display='none'; Codehighlighter1_646_652_Open_Text.style.display='none'; Codehighlighter1_646_652_Closed_Image.style.display='inline'; Codehighlighter1_646_652_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_646_652_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_646_652_Closed_Text.style.display='none'; Codehighlighter1_646_652_Open_Image.style.display='inline'; Codehighlighter1_646_652_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_646_652_Closed_Text">/**/</span><span id="Codehighlighter1_646_652_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">排序后</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">36</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">37</span><span style="color: #000000"><img id="Codehighlighter1_698_745_Open_Image" onclick="this.style.display='none'; Codehighlighter1_698_745_Open_Text.style.display='none'; Codehighlighter1_698_745_Closed_Image.style.display='inline'; Codehighlighter1_698_745_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_698_745_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_698_745_Closed_Text.style.display='none'; Codehighlighter1_698_745_Open_Image.style.display='inline'; Codehighlighter1_698_745_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_698_745_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_698_745_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">38</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;array[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;</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">39</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">40</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">41</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">42</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;array;<br /></span><span style="color: #008080">43</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">44</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">45</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">46</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;bubbleSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;array,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;length)<br /></span><span style="color: #008080">47</span><span style="color: #000000"><img id="Codehighlighter1_854_1154_Open_Image" onclick="this.style.display='none'; Codehighlighter1_854_1154_Open_Text.style.display='none'; Codehighlighter1_854_1154_Closed_Image.style.display='inline'; Codehighlighter1_854_1154_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_854_1154_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_854_1154_Closed_Text.style.display='none'; Codehighlighter1_854_1154_Open_Image.style.display='inline'; Codehighlighter1_854_1154_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_854_1154_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_854_1154_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">48</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;length;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">49</span><span style="color: #000000"><img id="Codehighlighter1_897_1152_Open_Image" onclick="this.style.display='none'; Codehighlighter1_897_1152_Open_Text.style.display='none'; Codehighlighter1_897_1152_Closed_Image.style.display='inline'; Codehighlighter1_897_1152_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_897_1152_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_897_1152_Closed_Text.style.display='none'; Codehighlighter1_897_1152_Open_Image.style.display='inline'; Codehighlighter1_897_1152_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_897_1152_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_897_1152_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">50</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;j&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;j&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;i;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">j)<br /></span><span style="color: #008080">51</span><span style="color: #000000"><img id="Codehighlighter1_952_1146_Open_Image" onclick="this.style.display='none'; Codehighlighter1_952_1146_Open_Text.style.display='none'; Codehighlighter1_952_1146_Closed_Image.style.display='inline'; Codehighlighter1_952_1146_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_952_1146_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_952_1146_Closed_Text.style.display='none'; Codehighlighter1_952_1146_Open_Image.style.display='inline'; Codehighlighter1_952_1146_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_952_1146_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_952_1146_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">52</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(array[j]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;array[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">53</span><span style="color: #000000"><img id="Codehighlighter1_1007_1136_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1007_1136_Open_Text.style.display='none'; Codehighlighter1_1007_1136_Closed_Image.style.display='inline'; Codehighlighter1_1007_1136_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1007_1136_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1007_1136_Closed_Text.style.display='none'; Codehighlighter1_1007_1136_Open_Image.style.display='inline'; Codehighlighter1_1007_1136_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1007_1136_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1007_1136_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">54</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[j];<br /></span><span style="color: #008080">55</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[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">56</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[j&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;temp;<br /></span><span style="color: #008080">57</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">58</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">59</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">60</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">61</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><br /><img src ="http://www.cppblog.com/zhuxin/aggbug/193694.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhuxin/" target="_blank">zhuxin</a> 2012-10-22 21:50 <a href="http://www.cppblog.com/zhuxin/articles/193694.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>插入排序(递归)</title><link>http://www.cppblog.com/zhuxin/articles/193692.html</link><dc:creator>zhuxin</dc:creator><author>zhuxin</author><pubDate>Mon, 22 Oct 2012 13:26:00 GMT</pubDate><guid>http://www.cppblog.com/zhuxin/articles/193692.html</guid><wfw:comment>http://www.cppblog.com/zhuxin/comments/193692.html</wfw:comment><comments>http://www.cppblog.com/zhuxin/articles/193692.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhuxin/comments/commentRss/193692.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhuxin/services/trackbacks/193692.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;插入排序可以如下改写成一个递归过程：为排序A[0 .. n - 1],先递归地排序A[0 ..&nbsp;n - 2],然后再将A[n - 1]插入到已排序的数组A[0 .. n - 2]中去。
<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><img id="Codehighlighter1_0_62_Open_Image" onclick="this.style.display='none'; Codehighlighter1_0_62_Open_Text.style.display='none'; Codehighlighter1_0_62_Closed_Image.style.display='inline'; Codehighlighter1_0_62_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_0_62_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_0_62_Closed_Text.style.display='none'; Codehighlighter1_0_62_Open_Image.style.display='inline'; Codehighlighter1_0_62_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_0_62_Closed_Text">/**/</span><span id="Codehighlighter1_0_62_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;名称：插入排序<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;最坏时间复杂度（降序）：O(n&nbsp;*&nbsp;n)<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;最好时间复杂度（升序）：O(n)<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" /></span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">time.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></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">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;insertSortRecursion(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">);<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></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">12</span><span style="color: #000000"><img id="Codehighlighter1_195_857_Open_Image" onclick="this.style.display='none'; Codehighlighter1_195_857_Open_Text.style.display='none'; Codehighlighter1_195_857_Closed_Image.style.display='inline'; Codehighlighter1_195_857_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_195_857_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_195_857_Closed_Text.style.display='none'; Codehighlighter1_195_857_Open_Image.style.display='inline'; Codehighlighter1_195_857_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_195_857_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_195_857_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n;<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">array;<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img id="Codehighlighter1_261_290_Open_Image" onclick="this.style.display='none'; Codehighlighter1_261_290_Open_Text.style.display='none'; Codehighlighter1_261_290_Closed_Image.style.display='inline'; Codehighlighter1_261_290_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_261_290_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_261_290_Closed_Text.style.display='none'; Codehighlighter1_261_290_Open_Image.style.display='inline'; Codehighlighter1_261_290_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;srand(time(NULL));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_261_290_Closed_Text">/**/</span><span id="Codehighlighter1_261_290_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">void&nbsp;srand(unsigned&nbsp;seed)；</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</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><span style="color: #000000"><img id="Codehighlighter1_313_841_Open_Image" onclick="this.style.display='none'; Codehighlighter1_313_841_Open_Text.style.display='none'; Codehighlighter1_313_841_Closed_Image.style.display='inline'; Codehighlighter1_313_841_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_313_841_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_313_841_Closed_Text.style.display='none'; Codehighlighter1_313_841_Open_Image.style.display='inline'; Codehighlighter1_313_841_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_313_841_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_313_841_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&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">19</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array&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">[n];<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img id="Codehighlighter1_369_378_Open_Image" onclick="this.style.display='none'; Codehighlighter1_369_378_Open_Text.style.display='none'; Codehighlighter1_369_378_Closed_Image.style.display='inline'; Codehighlighter1_369_378_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_369_378_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_369_378_Closed_Text.style.display='none'; Codehighlighter1_369_378_Open_Image.style.display='inline'; Codehighlighter1_369_378_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_369_378_Closed_Text">/**/</span><span id="Codehighlighter1_369_378_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">随机生成数组</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img id="Codehighlighter1_424_491_Open_Image" onclick="this.style.display='none'; Codehighlighter1_424_491_Open_Text.style.display='none'; Codehighlighter1_424_491_Closed_Image.style.display='inline'; Codehighlighter1_424_491_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_424_491_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_424_491_Closed_Text.style.display='none'; Codehighlighter1_424_491_Open_Image.style.display='inline'; Codehighlighter1_424_491_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_424_491_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_424_491_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img id="Codehighlighter1_463_481_Open_Image" onclick="this.style.display='none'; Codehighlighter1_463_481_Open_Text.style.display='none'; Codehighlighter1_463_481_Closed_Image.style.display='inline'; Codehighlighter1_463_481_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_463_481_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_463_481_Closed_Text.style.display='none'; Codehighlighter1_463_481_Open_Image.style.display='inline'; Codehighlighter1_463_481_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;rand()&nbsp;</span><span style="color: #000000">%</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_463_481_Closed_Text">/**/</span><span id="Codehighlighter1_463_481_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">int&nbsp;rand(void)；</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">25</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">26</span><span style="color: #000000"><img id="Codehighlighter1_502_508_Open_Image" onclick="this.style.display='none'; Codehighlighter1_502_508_Open_Text.style.display='none'; Codehighlighter1_502_508_Closed_Image.style.display='inline'; Codehighlighter1_502_508_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_502_508_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_502_508_Closed_Text.style.display='none'; Codehighlighter1_502_508_Open_Image.style.display='inline'; Codehighlighter1_502_508_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_502_508_Closed_Text">/**/</span><span id="Codehighlighter1_502_508_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">排序前</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">27</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img id="Codehighlighter1_554_601_Open_Image" onclick="this.style.display='none'; Codehighlighter1_554_601_Open_Text.style.display='none'; Codehighlighter1_554_601_Closed_Image.style.display='inline'; Codehighlighter1_554_601_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_554_601_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_554_601_Closed_Text.style.display='none'; Codehighlighter1_554_601_Open_Image.style.display='inline'; Codehighlighter1_554_601_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_554_601_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_554_601_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;array[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;</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">31</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">32</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">33</span><span style="color: #000000"><img id="Codehighlighter1_634_641_Open_Image" onclick="this.style.display='none'; Codehighlighter1_634_641_Open_Text.style.display='none'; Codehighlighter1_634_641_Closed_Image.style.display='inline'; Codehighlighter1_634_641_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_634_641_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_634_641_Closed_Text.style.display='none'; Codehighlighter1_634_641_Open_Image.style.display='inline'; Codehighlighter1_634_641_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_634_641_Closed_Text">/**/</span><span id="Codehighlighter1_634_641_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">插入排序</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">34</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertSortRecursion(array,&nbsp;n);<br /></span><span style="color: #008080">35</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">36</span><span style="color: #000000"><img id="Codehighlighter1_691_697_Open_Image" onclick="this.style.display='none'; Codehighlighter1_691_697_Open_Text.style.display='none'; Codehighlighter1_691_697_Closed_Image.style.display='inline'; Codehighlighter1_691_697_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_691_697_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_691_697_Closed_Text.style.display='none'; Codehighlighter1_691_697_Open_Image.style.display='inline'; Codehighlighter1_691_697_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_691_697_Closed_Text">/**/</span><span id="Codehighlighter1_691_697_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">排序后</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">37</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">38</span><span style="color: #000000"><img id="Codehighlighter1_743_790_Open_Image" onclick="this.style.display='none'; Codehighlighter1_743_790_Open_Text.style.display='none'; Codehighlighter1_743_790_Closed_Image.style.display='inline'; Codehighlighter1_743_790_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_743_790_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_743_790_Closed_Text.style.display='none'; Codehighlighter1_743_790_Open_Image.style.display='inline'; Codehighlighter1_743_790_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_743_790_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_743_790_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">39</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;array[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;</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">40</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">41</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">42</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">43</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;array;<br /></span><span style="color: #008080">44</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">45</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">46</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">47</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;insertSortRecursion(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;array,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;length)<br /></span><span style="color: #008080">48</span><span style="color: #000000"><img id="Codehighlighter1_908_1305_Open_Image" onclick="this.style.display='none'; Codehighlighter1_908_1305_Open_Text.style.display='none'; Codehighlighter1_908_1305_Closed_Image.style.display='inline'; Codehighlighter1_908_1305_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_908_1305_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_908_1305_Closed_Text.style.display='none'; Codehighlighter1_908_1305_Open_Image.style.display='inline'; Codehighlighter1_908_1305_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_908_1305_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_908_1305_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">49</span><span style="color: #000000"><img id="Codehighlighter1_914_931_Open_Image" onclick="this.style.display='none'; Codehighlighter1_914_931_Open_Text.style.display='none'; Codehighlighter1_914_931_Closed_Image.style.display='inline'; Codehighlighter1_914_931_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_914_931_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_914_931_Closed_Text.style.display='none'; Codehighlighter1_914_931_Open_Image.style.display='inline'; Codehighlighter1_914_931_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_914_931_Closed_Text">/**/</span><span id="Codehighlighter1_914_931_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">只有一个元素直接return</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">50</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</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">)<br /></span><span style="color: #008080">51</span><span style="color: #000000"><img id="Codehighlighter1_958_981_Open_Image" onclick="this.style.display='none'; Codehighlighter1_958_981_Open_Text.style.display='none'; Codehighlighter1_958_981_Closed_Image.style.display='inline'; Codehighlighter1_958_981_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_958_981_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_958_981_Closed_Text.style.display='none'; Codehighlighter1_958_981_Open_Image.style.display='inline'; Codehighlighter1_958_981_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_958_981_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_958_981_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">52</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;;<br /></span><span style="color: #008080">53</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">54</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;insertSortRecursion(array,&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">);<br /></span><span style="color: #008080">55</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(array[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;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;array[length&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">56</span><span style="color: #000000"><img id="Codehighlighter1_1078_1303_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1078_1303_Open_Text.style.display='none'; Codehighlighter1_1078_1303_Closed_Image.style.display='inline'; Codehighlighter1_1078_1303_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1078_1303_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1078_1303_Closed_Text.style.display='none'; Codehighlighter1_1078_1303_Open_Image.style.display='inline'; Codehighlighter1_1078_1303_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1078_1303_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1078_1303_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">57</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[length&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">58</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&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">59</span><span style="color: #000000"><img id="Codehighlighter1_1141_1150_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1141_1150_Open_Text.style.display='none'; Codehighlighter1_1141_1150_Closed_Image.style.display='inline'; Codehighlighter1_1141_1150_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1141_1150_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1141_1150_Closed_Text.style.display='none'; Codehighlighter1_1141_1150_Open_Image.style.display='inline'; Codehighlighter1_1141_1150_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1141_1150_Closed_Text">/**/</span><span id="Codehighlighter1_1141_1150_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">寻找插入位置</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">60</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&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;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;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">&amp;&amp;</span><span style="color: #000000">&nbsp;array[i]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;temp;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">i)<br /></span><span style="color: #008080">61</span><span style="color: #000000"><img id="Codehighlighter1_1221_1268_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1221_1268_Open_Text.style.display='none'; Codehighlighter1_1221_1268_Closed_Image.style.display='inline'; Codehighlighter1_1221_1268_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1221_1268_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1221_1268_Closed_Text.style.display='none'; Codehighlighter1_1221_1268_Open_Image.style.display='inline'; Codehighlighter1_1221_1268_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1221_1268_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1221_1268_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">62</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[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">=</span><span style="color: #000000">&nbsp;array[i];<br /></span><span style="color: #008080">63</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">64</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[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">=</span><span style="color: #000000">&nbsp;temp;<br /></span><span style="color: #008080">65</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">66</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">67</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><br /><img src ="http://www.cppblog.com/zhuxin/aggbug/193692.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhuxin/" target="_blank">zhuxin</a> 2012-10-22 21:26 <a href="http://www.cppblog.com/zhuxin/articles/193692.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>选择排序</title><link>http://www.cppblog.com/zhuxin/articles/193688.html</link><dc:creator>zhuxin</dc:creator><author>zhuxin</author><pubDate>Mon, 22 Oct 2012 13:04:00 GMT</pubDate><guid>http://www.cppblog.com/zhuxin/articles/193688.html</guid><wfw:comment>http://www.cppblog.com/zhuxin/comments/193688.html</wfw:comment><comments>http://www.cppblog.com/zhuxin/articles/193688.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhuxin/comments/commentRss/193688.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhuxin/services/trackbacks/193688.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;算法描述：首先找出A中的最小元素，并将其与A[0]中的元素交换。接着。找出A中的次最小元素，并将其与A[1]中的元素进行交换。对A中头n - 1个元素继续这一过程。 
<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><img id="Codehighlighter1_0_35_Open_Image" onclick="this.style.display='none'; Codehighlighter1_0_35_Open_Text.style.display='none'; Codehighlighter1_0_35_Closed_Image.style.display='inline'; Codehighlighter1_0_35_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_0_35_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_0_35_Closed_Text.style.display='none'; Codehighlighter1_0_35_Open_Image.style.display='inline'; Codehighlighter1_0_35_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_0_35_Closed_Text">/**/</span><span id="Codehighlighter1_0_35_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;名称：选择排序<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;时间复杂度：O(n&nbsp;*&nbsp;n)<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" /></span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">time.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></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;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;selectSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">);<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></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">11</span><span style="color: #000000"><img id="Codehighlighter1_159_812_Open_Image" onclick="this.style.display='none'; Codehighlighter1_159_812_Open_Text.style.display='none'; Codehighlighter1_159_812_Closed_Image.style.display='inline'; Codehighlighter1_159_812_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_159_812_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_159_812_Closed_Text.style.display='none'; Codehighlighter1_159_812_Open_Image.style.display='inline'; Codehighlighter1_159_812_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_159_812_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_159_812_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">12</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n;<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">array;<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img id="Codehighlighter1_225_254_Open_Image" onclick="this.style.display='none'; Codehighlighter1_225_254_Open_Text.style.display='none'; Codehighlighter1_225_254_Closed_Image.style.display='inline'; Codehighlighter1_225_254_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_225_254_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_225_254_Closed_Text.style.display='none'; Codehighlighter1_225_254_Open_Image.style.display='inline'; Codehighlighter1_225_254_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;srand(time(NULL));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_225_254_Closed_Text">/**/</span><span id="Codehighlighter1_225_254_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">void&nbsp;srand(unsigned&nbsp;seed)；</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">15</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">true</span><span style="color: #000000">)<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img id="Codehighlighter1_277_796_Open_Image" onclick="this.style.display='none'; Codehighlighter1_277_796_Open_Text.style.display='none'; Codehighlighter1_277_796_Closed_Image.style.display='inline'; Codehighlighter1_277_796_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_277_796_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_277_796_Closed_Text.style.display='none'; Codehighlighter1_277_796_Open_Image.style.display='inline'; Codehighlighter1_277_796_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_277_796_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_277_796_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&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">18</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array&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">[n];<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img id="Codehighlighter1_333_342_Open_Image" onclick="this.style.display='none'; Codehighlighter1_333_342_Open_Text.style.display='none'; Codehighlighter1_333_342_Closed_Image.style.display='inline'; Codehighlighter1_333_342_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_333_342_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_333_342_Closed_Text.style.display='none'; Codehighlighter1_333_342_Open_Image.style.display='inline'; Codehighlighter1_333_342_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_333_342_Closed_Text">/**/</span><span id="Codehighlighter1_333_342_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">随机生成数组</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">20</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">21</span><span style="color: #000000"><img id="Codehighlighter1_388_455_Open_Image" onclick="this.style.display='none'; Codehighlighter1_388_455_Open_Text.style.display='none'; Codehighlighter1_388_455_Closed_Image.style.display='inline'; Codehighlighter1_388_455_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_388_455_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_388_455_Closed_Text.style.display='none'; Codehighlighter1_388_455_Open_Image.style.display='inline'; Codehighlighter1_388_455_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_388_455_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_388_455_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img id="Codehighlighter1_427_445_Open_Image" onclick="this.style.display='none'; Codehighlighter1_427_445_Open_Text.style.display='none'; Codehighlighter1_427_445_Closed_Image.style.display='inline'; Codehighlighter1_427_445_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_427_445_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_427_445_Closed_Text.style.display='none'; Codehighlighter1_427_445_Open_Image.style.display='inline'; Codehighlighter1_427_445_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;rand()&nbsp;</span><span style="color: #000000">%</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_427_445_Closed_Text">/**/</span><span id="Codehighlighter1_427_445_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">int&nbsp;rand(void)；</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">23</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">25</span><span style="color: #000000"><img id="Codehighlighter1_466_472_Open_Image" onclick="this.style.display='none'; Codehighlighter1_466_472_Open_Text.style.display='none'; Codehighlighter1_466_472_Closed_Image.style.display='inline'; Codehighlighter1_466_472_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_466_472_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_466_472_Closed_Text.style.display='none'; Codehighlighter1_466_472_Open_Image.style.display='inline'; Codehighlighter1_466_472_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_466_472_Closed_Text">/**/</span><span id="Codehighlighter1_466_472_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">排序前</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">26</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">27</span><span style="color: #000000"><img id="Codehighlighter1_518_565_Open_Image" onclick="this.style.display='none'; Codehighlighter1_518_565_Open_Text.style.display='none'; Codehighlighter1_518_565_Closed_Image.style.display='inline'; Codehighlighter1_518_565_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_518_565_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_518_565_Closed_Text.style.display='none'; Codehighlighter1_518_565_Open_Image.style.display='inline'; Codehighlighter1_518_565_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_518_565_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_518_565_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;array[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;</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">30</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">31</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">32</span><span style="color: #000000"><img id="Codehighlighter1_598_605_Open_Image" onclick="this.style.display='none'; Codehighlighter1_598_605_Open_Text.style.display='none'; Codehighlighter1_598_605_Closed_Image.style.display='inline'; Codehighlighter1_598_605_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_598_605_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_598_605_Closed_Text.style.display='none'; Codehighlighter1_598_605_Open_Image.style.display='inline'; Codehighlighter1_598_605_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_598_605_Closed_Text">/**/</span><span id="Codehighlighter1_598_605_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">选择排序</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">33</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectSort(array,&nbsp;n);<br /></span><span style="color: #008080">34</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">35</span><span style="color: #000000"><img id="Codehighlighter1_646_652_Open_Image" onclick="this.style.display='none'; Codehighlighter1_646_652_Open_Text.style.display='none'; Codehighlighter1_646_652_Closed_Image.style.display='inline'; Codehighlighter1_646_652_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_646_652_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_646_652_Closed_Text.style.display='none'; Codehighlighter1_646_652_Open_Image.style.display='inline'; Codehighlighter1_646_652_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_646_652_Closed_Text">/**/</span><span id="Codehighlighter1_646_652_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">排序后</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">36</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">37</span><span style="color: #000000"><img id="Codehighlighter1_698_745_Open_Image" onclick="this.style.display='none'; Codehighlighter1_698_745_Open_Text.style.display='none'; Codehighlighter1_698_745_Closed_Image.style.display='inline'; Codehighlighter1_698_745_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_698_745_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_698_745_Closed_Text.style.display='none'; Codehighlighter1_698_745_Open_Image.style.display='inline'; Codehighlighter1_698_745_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_698_745_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_698_745_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">38</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;array[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;</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">39</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">40</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">41</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">42</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;array;<br /></span><span style="color: #008080">43</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">44</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">45</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">46</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;selectSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;array,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;length)<br /></span><span style="color: #008080">47</span><span style="color: #000000"><img id="Codehighlighter1_854_1322_Open_Image" onclick="this.style.display='none'; Codehighlighter1_854_1322_Open_Text.style.display='none'; Codehighlighter1_854_1322_Closed_Image.style.display='inline'; Codehighlighter1_854_1322_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_854_1322_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_854_1322_Closed_Text.style.display='none'; Codehighlighter1_854_1322_Open_Image.style.display='inline'; Codehighlighter1_854_1322_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_854_1322_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_854_1322_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">48</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;length;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">49</span><span style="color: #000000"><img id="Codehighlighter1_897_1320_Open_Image" onclick="this.style.display='none'; Codehighlighter1_897_1320_Open_Text.style.display='none'; Codehighlighter1_897_1320_Closed_Image.style.display='inline'; Codehighlighter1_897_1320_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_897_1320_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_897_1320_Closed_Text.style.display='none'; Codehighlighter1_897_1320_Open_Image.style.display='inline'; Codehighlighter1_897_1320_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_897_1320_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_897_1320_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">50</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;pivot&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;i;<br /></span><span style="color: #008080">51</span><span style="color: #000000"><img id="Codehighlighter1_930_974_Open_Image" onclick="this.style.display='none'; Codehighlighter1_930_974_Open_Text.style.display='none'; Codehighlighter1_930_974_Closed_Image.style.display='inline'; Codehighlighter1_930_974_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_930_974_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_930_974_Closed_Text.style.display='none'; Codehighlighter1_930_974_Open_Image.style.display='inline'; Codehighlighter1_930_974_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_930_974_Closed_Text">/**/</span><span id="Codehighlighter1_930_974_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">从array[j&nbsp;..&nbsp;length&nbsp;-&nbsp;1]选择一个最小的与array[i]交换</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">52</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;j&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;j&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;length;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">j)<br /></span><span style="color: #008080">53</span><span style="color: #000000"><img id="Codehighlighter1_1029_1135_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1029_1135_Open_Text.style.display='none'; Codehighlighter1_1029_1135_Closed_Image.style.display='inline'; Codehighlighter1_1029_1135_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1029_1135_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1029_1135_Closed_Text.style.display='none'; Codehighlighter1_1029_1135_Open_Image.style.display='inline'; Codehighlighter1_1029_1135_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1029_1135_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1029_1135_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">54</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(array[j]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;array[pivot])<br /></span><span style="color: #008080">55</span><span style="color: #000000"><img id="Codehighlighter1_1084_1125_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1084_1125_Open_Text.style.display='none'; Codehighlighter1_1084_1125_Closed_Image.style.display='inline'; Codehighlighter1_1084_1125_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1084_1125_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1084_1125_Closed_Text.style.display='none'; Codehighlighter1_1084_1125_Open_Image.style.display='inline'; Codehighlighter1_1084_1125_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1084_1125_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1084_1125_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">56</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pivot&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;j;<br /></span><span style="color: #008080">57</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">58</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">59</span><span style="color: #000000"><img id="Codehighlighter1_1145_1167_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1145_1167_Open_Text.style.display='none'; Codehighlighter1_1145_1167_Closed_Image.style.display='inline'; Codehighlighter1_1145_1167_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1145_1167_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1145_1167_Closed_Text.style.display='none'; Codehighlighter1_1145_1167_Open_Image.style.display='inline'; Codehighlighter1_1145_1167_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1145_1167_Closed_Text">/**/</span><span id="Codehighlighter1_1145_1167_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">如果最小的不是array[i],则交换</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">60</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(pivot&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;i)<br /></span><span style="color: #008080">61</span><span style="color: #000000"><img id="Codehighlighter1_1201_1314_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1201_1314_Open_Text.style.display='none'; Codehighlighter1_1201_1314_Closed_Image.style.display='inline'; Codehighlighter1_1201_1314_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1201_1314_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1201_1314_Closed_Text.style.display='none'; Codehighlighter1_1201_1314_Open_Image.style.display='inline'; Codehighlighter1_1201_1314_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1201_1314_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1201_1314_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">62</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[i];<br /></span><span style="color: #008080">63</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[pivot];<br /></span><span style="color: #008080">64</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[pivot]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;temp;<br /></span><span style="color: #008080">65</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">66</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">67</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">68</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><br /><br /><img src ="http://www.cppblog.com/zhuxin/aggbug/193688.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhuxin/" target="_blank">zhuxin</a> 2012-10-22 21:04 <a href="http://www.cppblog.com/zhuxin/articles/193688.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>归并排序</title><link>http://www.cppblog.com/zhuxin/articles/193639.html</link><dc:creator>zhuxin</dc:creator><author>zhuxin</author><pubDate>Sun, 21 Oct 2012 15:46:00 GMT</pubDate><guid>http://www.cppblog.com/zhuxin/articles/193639.html</guid><wfw:comment>http://www.cppblog.com/zhuxin/comments/193639.html</wfw:comment><comments>http://www.cppblog.com/zhuxin/articles/193639.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhuxin/comments/commentRss/193639.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhuxin/services/trackbacks/193639.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->&nbsp;&nbsp;1/**//*&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;名称：归并排序&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;最坏...&nbsp;&nbsp;<a href='http://www.cppblog.com/zhuxin/articles/193639.html'>阅读全文</a><img src ="http://www.cppblog.com/zhuxin/aggbug/193639.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhuxin/" target="_blank">zhuxin</a> 2012-10-21 23:46 <a href="http://www.cppblog.com/zhuxin/articles/193639.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>插入排序</title><link>http://www.cppblog.com/zhuxin/articles/193638.html</link><dc:creator>zhuxin</dc:creator><author>zhuxin</author><pubDate>Sun, 21 Oct 2012 15:40:00 GMT</pubDate><guid>http://www.cppblog.com/zhuxin/articles/193638.html</guid><wfw:comment>http://www.cppblog.com/zhuxin/comments/193638.html</wfw:comment><comments>http://www.cppblog.com/zhuxin/articles/193638.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhuxin/comments/commentRss/193638.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhuxin/services/trackbacks/193638.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;一般来说，插入排序都采用in-place在数组上实现。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;具体算法描述如下：<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;从第一个元素开始，该元素可以认为已经被排序<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;取出下一个元素，在已经排序的元素序列中从后向前扫描<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果该元素（已排序）大于新元素，将该元素移到下一位置<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;重复步骤3，直到找到已排序的元素小于或者等于新元素的位置<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;将新元素插入到该位置后<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;重复步骤2~5<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果比较操作的代价比交换操作大的话，可以采用二分查找法来减少比较操作的数目。该算法可以认为是插入排序的一个变种，称为二分查找排序。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<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><img id="Codehighlighter1_0_62_Open_Image" onclick="this.style.display='none'; Codehighlighter1_0_62_Open_Text.style.display='none'; Codehighlighter1_0_62_Closed_Image.style.display='inline'; Codehighlighter1_0_62_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_0_62_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_0_62_Closed_Text.style.display='none'; Codehighlighter1_0_62_Open_Image.style.display='inline'; Codehighlighter1_0_62_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_0_62_Closed_Text">/**/</span><span id="Codehighlighter1_0_62_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;名称：插入排序<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;最坏时间复杂度（降序）：O(n&nbsp;*&nbsp;n)<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;最好时间复杂度（升序）：O(n)<br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #008000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" /></span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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;7</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">time.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#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;9</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></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">10</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;insertSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">);<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></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">12</span><span style="color: #000000"><img id="Codehighlighter1_186_839_Open_Image" onclick="this.style.display='none'; Codehighlighter1_186_839_Open_Text.style.display='none'; Codehighlighter1_186_839_Closed_Image.style.display='inline'; Codehighlighter1_186_839_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_186_839_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_186_839_Closed_Text.style.display='none'; Codehighlighter1_186_839_Open_Image.style.display='inline'; Codehighlighter1_186_839_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_186_839_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_186_839_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">13</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n;<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">array;<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img id="Codehighlighter1_252_281_Open_Image" onclick="this.style.display='none'; Codehighlighter1_252_281_Open_Text.style.display='none'; Codehighlighter1_252_281_Closed_Image.style.display='inline'; Codehighlighter1_252_281_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_252_281_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_252_281_Closed_Text.style.display='none'; Codehighlighter1_252_281_Open_Image.style.display='inline'; Codehighlighter1_252_281_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;srand(time(NULL));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_252_281_Closed_Text">/**/</span><span id="Codehighlighter1_252_281_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">void&nbsp;srand(unsigned&nbsp;seed)；</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">16</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</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><span style="color: #000000"><img id="Codehighlighter1_304_823_Open_Image" onclick="this.style.display='none'; Codehighlighter1_304_823_Open_Text.style.display='none'; Codehighlighter1_304_823_Closed_Image.style.display='inline'; Codehighlighter1_304_823_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_304_823_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_304_823_Closed_Text.style.display='none'; Codehighlighter1_304_823_Open_Image.style.display='inline'; Codehighlighter1_304_823_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_304_823_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_304_823_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">18</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&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">19</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array&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">[n];<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img id="Codehighlighter1_360_369_Open_Image" onclick="this.style.display='none'; Codehighlighter1_360_369_Open_Text.style.display='none'; Codehighlighter1_360_369_Closed_Image.style.display='inline'; Codehighlighter1_360_369_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_360_369_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_360_369_Closed_Text.style.display='none'; Codehighlighter1_360_369_Open_Image.style.display='inline'; Codehighlighter1_360_369_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_360_369_Closed_Text">/**/</span><span id="Codehighlighter1_360_369_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">随机生成数组</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">21</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img id="Codehighlighter1_415_482_Open_Image" onclick="this.style.display='none'; Codehighlighter1_415_482_Open_Text.style.display='none'; Codehighlighter1_415_482_Closed_Image.style.display='inline'; Codehighlighter1_415_482_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_415_482_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_415_482_Closed_Text.style.display='none'; Codehighlighter1_415_482_Open_Image.style.display='inline'; Codehighlighter1_415_482_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_415_482_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_415_482_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">23</span><span style="color: #000000"><img id="Codehighlighter1_454_472_Open_Image" onclick="this.style.display='none'; Codehighlighter1_454_472_Open_Text.style.display='none'; Codehighlighter1_454_472_Closed_Image.style.display='inline'; Codehighlighter1_454_472_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_454_472_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_454_472_Closed_Text.style.display='none'; Codehighlighter1_454_472_Open_Image.style.display='inline'; Codehighlighter1_454_472_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;rand()&nbsp;</span><span style="color: #000000">%</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_454_472_Closed_Text">/**/</span><span id="Codehighlighter1_454_472_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">int&nbsp;rand(void)；</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">24</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">25</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">26</span><span style="color: #000000"><img id="Codehighlighter1_493_499_Open_Image" onclick="this.style.display='none'; Codehighlighter1_493_499_Open_Text.style.display='none'; Codehighlighter1_493_499_Closed_Image.style.display='inline'; Codehighlighter1_493_499_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_493_499_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_493_499_Closed_Text.style.display='none'; Codehighlighter1_493_499_Open_Image.style.display='inline'; Codehighlighter1_493_499_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_493_499_Closed_Text">/**/</span><span id="Codehighlighter1_493_499_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">排序前</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">27</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img id="Codehighlighter1_545_592_Open_Image" onclick="this.style.display='none'; Codehighlighter1_545_592_Open_Text.style.display='none'; Codehighlighter1_545_592_Closed_Image.style.display='inline'; Codehighlighter1_545_592_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_545_592_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_545_592_Closed_Text.style.display='none'; Codehighlighter1_545_592_Open_Image.style.display='inline'; Codehighlighter1_545_592_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_545_592_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_545_592_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">29</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;array[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;</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">30</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">31</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">32</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">33</span><span style="color: #000000"><img id="Codehighlighter1_625_632_Open_Image" onclick="this.style.display='none'; Codehighlighter1_625_632_Open_Text.style.display='none'; Codehighlighter1_625_632_Closed_Image.style.display='inline'; Codehighlighter1_625_632_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_625_632_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_625_632_Closed_Text.style.display='none'; Codehighlighter1_625_632_Open_Image.style.display='inline'; Codehighlighter1_625_632_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_625_632_Closed_Text">/**/</span><span id="Codehighlighter1_625_632_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">插入排序</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">34</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertSort(array,&nbsp;n);<br /></span><span style="color: #008080">35</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">36</span><span style="color: #000000"><img id="Codehighlighter1_673_679_Open_Image" onclick="this.style.display='none'; Codehighlighter1_673_679_Open_Text.style.display='none'; Codehighlighter1_673_679_Closed_Image.style.display='inline'; Codehighlighter1_673_679_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_673_679_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_673_679_Closed_Text.style.display='none'; Codehighlighter1_673_679_Open_Image.style.display='inline'; Codehighlighter1_673_679_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_673_679_Closed_Text">/**/</span><span id="Codehighlighter1_673_679_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">排序后</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">37</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&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;</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;n;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">38</span><span style="color: #000000"><img id="Codehighlighter1_725_772_Open_Image" onclick="this.style.display='none'; Codehighlighter1_725_772_Open_Text.style.display='none'; Codehighlighter1_725_772_Closed_Image.style.display='inline'; Codehighlighter1_725_772_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_725_772_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_725_772_Closed_Text.style.display='none'; Codehighlighter1_725_772_Open_Image.style.display='inline'; Codehighlighter1_725_772_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_725_772_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_725_772_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">39</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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;array[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;</span><span style="color: #000000">'</span><span style="color: #000000">;<br /></span><span style="color: #008080">40</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">41</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;endl;<br /></span><span style="color: #008080">42</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">43</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;array;<br /></span><span style="color: #008080">44</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">45</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">46</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">47</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;insertSort(</span><span style="color: #0000ff">int</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;array,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;length)<br /></span><span style="color: #008080">48</span><span style="color: #000000"><img id="Codehighlighter1_881_1292_Open_Image" onclick="this.style.display='none'; Codehighlighter1_881_1292_Open_Text.style.display='none'; Codehighlighter1_881_1292_Closed_Image.style.display='inline'; Codehighlighter1_881_1292_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_881_1292_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_881_1292_Closed_Text.style.display='none'; Codehighlighter1_881_1292_Open_Image.style.display='inline'; Codehighlighter1_881_1292_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_881_1292_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_881_1292_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">49</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;length;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">i)<br /></span><span style="color: #008080">50</span><span style="color: #000000"><img id="Codehighlighter1_924_1290_Open_Image" onclick="this.style.display='none'; Codehighlighter1_924_1290_Open_Text.style.display='none'; Codehighlighter1_924_1290_Closed_Image.style.display='inline'; Codehighlighter1_924_1290_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_924_1290_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_924_1290_Closed_Text.style.display='none'; Codehighlighter1_924_1290_Open_Image.style.display='inline'; Codehighlighter1_924_1290_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_924_1290_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_924_1290_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">51</span><span style="color: #000000"><img id="Codehighlighter1_934_954_Open_Image" onclick="this.style.display='none'; Codehighlighter1_934_954_Open_Text.style.display='none'; Codehighlighter1_934_954_Closed_Image.style.display='inline'; Codehighlighter1_934_954_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_934_954_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_934_954_Closed_Text.style.display='none'; Codehighlighter1_934_954_Open_Image.style.display='inline'; Codehighlighter1_934_954_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_934_954_Closed_Text">/**/</span><span id="Codehighlighter1_934_954_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">[0&nbsp;-&nbsp;(i&nbsp;-&nbsp;1）]已排好序</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">52</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(array[i]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;array[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">53</span><span style="color: #000000"><img id="Codehighlighter1_1001_1284_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1001_1284_Open_Text.style.display='none'; Codehighlighter1_1001_1284_Closed_Image.style.display='inline'; Codehighlighter1_1001_1284_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1001_1284_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1001_1284_Closed_Text.style.display='none'; Codehighlighter1_1001_1284_Open_Image.style.display='inline'; Codehighlighter1_1001_1284_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1001_1284_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1001_1284_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">54</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;temp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;array[i];<br /></span><span style="color: #008080">55</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j;<br /></span><span style="color: #008080">56</span><span style="color: #000000"><img id="Codehighlighter1_1067_1087_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1067_1087_Open_Text.style.display='none'; Codehighlighter1_1067_1087_Closed_Image.style.display='inline'; Codehighlighter1_1067_1087_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1067_1087_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1067_1087_Closed_Text.style.display='none'; Codehighlighter1_1067_1087_Open_Image.style.display='inline'; Codehighlighter1_1067_1087_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1067_1087_Closed_Text">/**/</span><span id="Codehighlighter1_1067_1087_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">比array[i]大的元素依次后移</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">57</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&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&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;j&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;array[j]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;temp;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">j)<br /></span><span style="color: #008080">58</span><span style="color: #000000"><img id="Codehighlighter1_1161_1217_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1161_1217_Open_Text.style.display='none'; Codehighlighter1_1161_1217_Closed_Image.style.display='inline'; Codehighlighter1_1161_1217_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1161_1217_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1161_1217_Closed_Text.style.display='none'; Codehighlighter1_1161_1217_Open_Image.style.display='inline'; Codehighlighter1_1161_1217_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1161_1217_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1161_1217_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">59</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: #008080">60</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[j&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;array[j];<br /></span><span style="color: #008080">61</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">62</span><span style="color: #000000"><img id="Codehighlighter1_1231_1241_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1231_1241_Open_Text.style.display='none'; Codehighlighter1_1231_1241_Closed_Image.style.display='inline'; Codehighlighter1_1231_1241_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1231_1241_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1231_1241_Closed_Text.style.display='none'; Codehighlighter1_1231_1241_Open_Image.style.display='inline'; Codehighlighter1_1231_1241_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1231_1241_Closed_Text">/**/</span><span id="Codehighlighter1_1231_1241_Open_Text"><span style="color: #008000">/*</span><span style="color: #008000">赋给相应的元素</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br /></span><span style="color: #008080">63</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[j&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;temp;<br /></span><span style="color: #008080">64</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">65</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">66</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">67</span><span style="color: #000000"><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#000000" face="Verdana">如果目标是把n个元素的序列升序排列，那么采用插入排序存在最好情况和最坏情况。最好情况就是，序列已经是升序排列了，在这种情况下，需要进行的比较操作需(n-1)次即可。最坏情况就是，序列是降序排列，那么此时需要进行的比较共有n(n-1)/2次。插入排序的赋值操作是比较操作的次数减去(n-1)次。平均来说插入排序算法复杂度为O(n2)。因而，插入排序不适合对于数据量比较大的排序应用。但是，如果需要排序的数据量很小，例如，量级小于千，那么插入排序还是一个不错的选择。 插入排序在工业级库中也有着广泛的应用，在STL的sort算法和stdlib的qsort算法中，都将插入排序作为快速排序的补充，用于少量元素的排序（通常为8个或以下）。<br /></font><img src ="http://www.cppblog.com/zhuxin/aggbug/193638.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhuxin/" target="_blank">zhuxin</a> 2012-10-21 23:40 <a href="http://www.cppblog.com/zhuxin/articles/193638.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>