<p style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;text-align: center;" data-mpa-powered-by="yiban.io"><br></p><p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p><p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p><p style="max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;font-size: 16px;letter-spacing: 0.544px;text-decoration: underline;color: rgb(255, 41, 65);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;box-sizing: border-box !important;overflow-wrap: break-word !important;"><a href="https://mp.weixin.qq.com/s?__biz=MzU5NTgzMDYyMA==&mid=2247484965&idx=1&sn=0a4226d6e2445a06e7f2f79c6d9ae866&chksm=fe6ab416c91d3d00a6ef9f212d5540736d7dbad445abf5f742e86f29c354504a896081c84321&token=647310012&lang=zh_CN&scene=21#wechat_redirect" target="_blank" data-linktype="2" style="color: rgb(255, 41, 65);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);cursor: pointer;max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;">☞ 程序员进阶必备资源免费送「21种技术方向!」 ☜</a></strong></p><p style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;"><br></p><blockquote style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;"><p style="max-width: 100%;min-height: 1em;color: rgba(0, 0, 0, 0.5);font-size: 15px;white-space: normal;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">作者:KaelQ,</span></p><p style="max-width: 100%;min-height: 1em;color: rgba(0, 0, 0, 0.5);font-size: 15px;white-space: normal;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: left;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">www.jianshu.com/p/5e171281a387</span></p></blockquote><p style="white-space: normal;font-variant-ligatures: normal;orphans: 2;widows: 2;"><span style="font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size: 15px;"></span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">1.直接插入排序</strong></span></p><blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="31" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">经常碰到这样一类排序问题:把新的数据插入到已经排好的数据列中。</p></section></blockquote><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将第一个数和第二个数排序,然后构成一个有序序列</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将第三个数插入进去,构成一个新的有序序列。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">对第四个数、第五个数……直到最后一个数,重复第二步。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="1.033282904689864" data-s="300,640" data-type="jpeg" data-w="661" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/eZzl4LXykQyU3JG7ZIqlc7Da4r92CygtP3mSkn6dUXaAF5icACpodCvwtyj8WctFicMLBpPVxpWvriczbCsNr5GsA/640?wx_fmt=jpeg" style="box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible !important; width: 661px !important; height: auto !important;" _width="661px" src="https://img2018.cnblogs.com/blog/1112483/201910/1112483-20191007132647000-1097259690.png" crossorigin="anonymous" data-fail="0"></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何写写成代码:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">首先设定插入次数,即循环次数,for(int i=1;i<length;i++),1个数的那次不用插入。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">设定插入数和得到已经排好序列的最后一个数的位数。insertNum和j=i-1。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">从最后一个数开始向前循环,如果插入数小于当前数,就将当前数向后移动一位。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将当前数放置到空着的位置,即j+1。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="hljs java" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span><span class="hljs-function"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function"> </span><span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-title">insertSort</span></span></span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-params">(</span></span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-params"><span class="hljs-keyword">int</span></span></span></span><span class="hljs-function"><span class="hljs-params">[] a)</span></span></span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> length=a.length;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//数组长度,将这个提取出来是为了提高速度。</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment"> insertNum;</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//要插入的数</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">for</span></span><span class="hljs-comment">(</span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment"> i=</span><span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">1</span></span><span class="hljs-comment">;i<length;i++){</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//插入的次数</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> insertNum=a[i];</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//要插入的数</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment"> j=i</span><span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">-1</span></span><span class="hljs-comment">;</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//已经排序好的序列元素个数</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">while</span></span><span class="hljs-comment">(j>=</span><span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">0</span></span><span class="hljs-comment">&&a[j]>insertNum){</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//序列从后到前循环,将大于insertNum的数向后移动一格</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> a[j+</span><span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">1</span></span><span class="hljs-comment">]=a[j];</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//元素移动一格</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> j--;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> a[j+</span><span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">1</span></span><span class="hljs-comment">]=insertNum;</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//将需要插入的数放在要插入的位置。</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span></code></pre><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">2.希尔排序</span></strong></span></p><blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="18" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">对于直接插入排序问题,数据量巨大时。</p></section></blockquote><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将数的个数设为n,取奇数k=n/2,将下标差值为k的书分为一组,构成有序序列。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">再取k=k/2 ,将下标差值为k的书分为一组,构成有序序列。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第二步,直到k=1执行简单插入排序。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="1.332618025751073" data-s="300,640" data-type="jpeg" data-w="466" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/eZzl4LXykQyU3JG7ZIqlc7Da4r92CygtMMTXh803ed4nr1m1ffPU5lUicxCnhsfgb4PD5iabK8U1VUbt2URWSjzA/640?wx_fmt=jpeg" style="box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible !important; width: 466px !important; height: auto !important;" _width="466px" src="https://img2018.cnblogs.com/blog/1112483/201910/1112483-20191007132703686-449269339.png" crossorigin="anonymous" data-fail="0"></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何写成代码:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">首先确定分的组数。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">然后对组中元素进行插入排序。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">然后将length/2,重复1,2步,直到length=0为止。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="hljs java" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span><span class="hljs-function"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function"> </span><span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-title">sheelSort</span></span></span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-params">(</span></span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-params"><span class="hljs-keyword">int</span></span></span></span><span class="hljs-function"><span class="hljs-params">[] a)</span></span></span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> d = a.length;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">while</span></span> (d!=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> d=d/<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">2</span></span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> x = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>; x < d; x++) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//分的组数</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">for</span></span><span class="hljs-comment"> (</span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment"> i = x + d; i < a.length; i += d) {</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//组中的元素,从第二个数开始</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment"> j = i - d;</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//j为有序序列最后一位的位数</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment"> temp = a[i];</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//要插入的元素</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">for</span></span><span class="hljs-comment"> (; j >= </span><span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">0</span></span><span class="hljs-comment"> && temp < a[j]; j -= d) {</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//从后往前遍历。</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> a[j + d] = a[j];</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//向后移动d位</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> a[j + d] = temp;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span></code></pre><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">3.简单选择排序</strong></span></p><blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="17" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">常用于取序列中最大最小的几个数时。</p></section></blockquote><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">(如果每次比较都交换,那么就是交换排序;如果每次比较完一个循环再交换,就是简单选择排序。)</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">遍历整个序列,将最小的数放在最前面。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">遍历剩下的序列,将最小的数放在最前面。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第二步,直到只剩下一个数。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="1.0056497175141244" data-s="300,640" data-type="jpeg" data-w="531" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/eZzl4LXykQyU3JG7ZIqlc7Da4r92CygticgADAzBUqgTpagMvzRa7RT54SicY8sqWpfHicibbjJwPlQ4FaticoMdq3A/640?wx_fmt=jpeg" style="box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible !important; width: 531px !important; height: auto !important;" _width="531px" src="https://img2018.cnblogs.com/blog/1112483/201910/1112483-20191007132723841-2135341889.png" crossorigin="anonymous" data-fail="0"></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何写成代码:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">首先确定循环次数,并且记住当前数字和当前位置。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将当前位置后面所有的数与当前数字进行对比,小数赋值给key,并记住小数的位置。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">比对完成后,将最小的值与第一个数的值交换。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复2、3步。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="hljs java" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span><span class="hljs-function"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function"> </span><span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-title">selectSort</span></span></span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-params">(</span></span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-params"><span class="hljs-keyword">int</span></span></span></span><span class="hljs-function"><span class="hljs-params">[] a)</span></span></span><span class="hljs-function"> </span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> length = a.length;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> i = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>; i < length; i++) {<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//循环次数</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment"> key = a[i];</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment"> position=i;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">for</span></span><span class="hljs-comment"> (</span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">int</span></span><span class="hljs-comment"> j = i + </span><span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">1</span></span><span class="hljs-comment">; j < length; j++) {</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//选出最小的值和位置</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">if</span></span><span class="hljs-comment"> (a[j] < key) {</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> key = a[j];</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> position = j;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> a[position]=a[i];</span><span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment">//交换位置</span></span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> a[i]=key;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-comment"> }</span></code></pre><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">4.堆排序</span></strong></span></p><blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="11" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">对简单选择排序的优化。</p></section></blockquote><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将序列构建成大顶堆。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将根节点与最后一个节点交换,然后断开最后一个节点。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第一、二步,直到所有节点断开。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="0.7598752598752598" data-s="300,640" data-type="jpeg" data-w="962" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/eZzl4LXykQyU3JG7ZIqlc7Da4r92CygthAPBPkzzYicWiaPJrtK5RniaIn6smKud1dBU4nekWLibSe1icdDlxt1jWeA/640?wx_fmt=jpeg" style="box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible !important; width: 677px !important; height: auto !important;" _width="677px" src="https://img2018.cnblogs.com/blog/1112483/201910/1112483-20191007132745359-296123283.png" crossorigin="anonymous" data-fail="0"></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="hljs perl" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">heapSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span>[] a)</span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> System.out.println(<span class="" style="max-width: 100%;color: rgb(152, 195, 121);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-string">"开始排序"</span></span>);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> arrayLength=a.length;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>循环建堆</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span>(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> i=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>;i<arrayLength<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-<span class="hljs-number">1</span></span>;i++){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>建堆</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> buildMaxHeap(a,arrayLength<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-<span class="hljs-number">1</span></span>-i);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>交换堆顶和最后一个元素</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> swap(a,<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>,arrayLength<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-<span class="hljs-number">1</span></span>-i);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> System.out.println(Arrays.toString(a));<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">private</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">swap</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span>[] data, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> i, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> j)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span> TODO Auto-generated method stub</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> tmp=data[i];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> data[i]=data[j];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> data[j]=tmp;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//对data数组从<span class="hljs-number">0</span>到lastIndex建大顶堆</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">private</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">buildMaxHeap</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span>[] data, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> lastIndex)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span> TODO Auto-generated method stub</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//从lastIndex处节点(最后一个节点)的父节点开始</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span>(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> i=(lastIndex<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-<span class="hljs-number">1</span></span>)/<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">2</span></span>;i>=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>;i--){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//k</span>保存正在判断的节点</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> k=i;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>如果当前k节点的子节点存在</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">while</span></span>(k*<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">2</span></span>+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span><=lastIndex){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//k</span>节点的左子节点的索引</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> biggerIndex=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">2</span></span>*k+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>如果biggerIndex小于lastIndex,即biggerIndex+<span class="hljs-number">1</span>代表的k节点的右子节点存在</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span>(biggerIndex<lastIndex){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>若果右子节点的值较大</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span>(data[biggerIndex]<data[biggerIndex+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>]){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//bigger</span>Index总是记录较大子节点的索引</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> biggerIndex++;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//如果k节点的值小于其较大的子节点的值</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span>(data[k]<data[biggerIndex]){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>交换他们</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> swap(data,k,biggerIndex);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>将biggerIndex赋予k,开始<span class="hljs-keyword">while</span>循环的下一次循环,重新保证k节点的值大于其左右子节点的值</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> k=biggerIndex;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">else</span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">break</span></span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">5.冒泡排序</span></strong></span></p><blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="5" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">一般不用。</p></section></blockquote><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将序列中所有元素两两比较,将最大的放在最后面。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将剩余序列中所有元素两两比较,将最大的放在最后面。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第二步,直到只剩下一个数。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="1.5909090909090908" data-s="300,640" data-type="jpeg" data-w="396" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/eZzl4LXykQyU3JG7ZIqlc7Da4r92CygtiaJh9wtia8y1jrqNOZ8zxJ9nuqiagSMJxxQSMPLLPic0ofFCm4Rf8m0TUw/640?wx_fmt=jpeg" style="box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible !important; width: 396px !important; height: auto !important;" _width="396px" src="https://img2018.cnblogs.com/blog/1112483/201910/1112483-20191007132809269-482389981.png" crossorigin="anonymous" data-fail="0"></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">如何写成代码:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">设置循环次数。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">设置开始比较的位数,和结束的位数。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">两两比较,将最小的放到前面去。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复2、3步,直到循环次数完毕。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="hljs java" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span><span class="hljs-function"> </span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span><span class="hljs-function"> </span><span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-title">bubbleSort</span></span></span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-params">(</span></span><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-function"><span class="hljs-params"><span class="hljs-keyword">int</span></span></span></span><span class="hljs-function"><span class="hljs-params">[] a)</span></span></span></span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> length=a.length;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> temp;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span>(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> i=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>;i<a.length;i++){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span>(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> j=<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>;j<a.length-i<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;">-<span class="hljs-number">1</span></span>;j++){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span>(a[j]>a[j+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>]){<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> temp=a[j];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[j]=a[j+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> a[j+<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>]=temp;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">6.快速排序</span></strong></span></p><blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="8" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">要求时间最快时。</p></section></blockquote><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">选择第一个数为p,小于p的数放在左边,大于p的数放在右边。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">递归的将p左边和右边的数都按照第一步进行,直到不能递归。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="0.6772277227722773" data-s="300,640" data-type="jpeg" data-w="505" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/eZzl4LXykQyU3JG7ZIqlc7Da4r92Cygtic8ANyLEem9Tic6j3OFGFJHe2o3vxyhhI7OQkK48CxS135TQVKXklu2A/640?wx_fmt=jpeg" style="box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible !important; width: 505px !important; height: auto !important;" _width="505px" src="https://img2018.cnblogs.com/blog/1112483/201910/1112483-20191007132826541-1200137466.png" crossorigin="anonymous" data-fail="0"></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="hljs ruby" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">static</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">quickSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span>[] numbers, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> start, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> <span class="hljs-keyword">end</span>)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span> (start < <span class="hljs-keyword">end</span>) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> base = numbers[start]; <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span> 选定的基准值(第一个数值作为基准值)</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> temp; <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span> 记录临时中间值</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">int</span> i = start, j = <span class="hljs-keyword">end</span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">do</span></span> {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">while</span></span> ((numbers[i] < base) && (i < <span class="hljs-keyword">end</span>))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> i++;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">while</span></span> ((numbers[j] > base) && (j > start))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> j--;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span> (i <= j) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> temp = numbers[i];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> numbers[i] = numbers[j];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> numbers[j] = temp;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> i++;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> j--;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> } <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">while</span></span> (i <= j);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span> (start < j)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> quickSort(numbers, start, j);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span> (<span class="hljs-keyword">end</span> > i)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> quickSort(numbers, i, <span class="hljs-keyword">end</span>);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">}</code></pre><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">7.归并排序</span></strong></span></p><blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="31" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">速度仅次于快排,内存少的时候使用,可以进行并行计算的时候使用。</p></section></blockquote><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">选择相邻两个数组成一个有序序列。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">选择相邻的两个有序序列组成一个有序序列。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">重复第二步,直到全部组成一个有序序列。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="0.6836734693877551" data-s="300,640" data-type="jpeg" data-w="490" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/eZzl4LXykQyU3JG7ZIqlc7Da4r92CygtVHdWVUypCMrqQECicFwX69icfdNOMK7ibdicfYS3synib3fw0C7ia5tic2xgg/640?wx_fmt=jpeg" style="box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible !important; width: 490px !important; height: auto !important;" _width="490px" src="https://img2018.cnblogs.com/blog/1112483/201910/1112483-20191007132845010-1529986988.png" crossorigin="anonymous" data-fail="0"></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="hljs perl" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">static</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">mergeSort</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span>[] numbers, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> left, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> right)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> t = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span> 每组元素个数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> size = right - left + <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">while</span></span> (t < size) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> <span class="hljs-keyword">s</span> = t;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span> 本次循环每组元素个数</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> t = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">2</span></span> * <span class="hljs-keyword">s</span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> i = left;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">while</span></span> (i + (t - <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>) < size) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> merge(numbers, i, i + (<span class="hljs-keyword">s</span> - <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>), i + (t - <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>));<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> i += t;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span> (i + (<span class="hljs-keyword">s</span> - <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>) < right)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> merge(numbers, i, i + (<span class="hljs-keyword">s</span> - <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>), right);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">}<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">private</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">static</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;">merge</span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span>[] data, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> p, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> <span class="hljs-keyword">q</span>, <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> r)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span>[] B = <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">new</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span>[data.length];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> <span class="hljs-keyword">s</span> = p;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> t = <span class="hljs-keyword">q</span> + <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> k = p;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">while</span></span> (<span class="hljs-keyword">s</span> <= <span class="hljs-keyword">q</span> && t <= r) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span> (data[<span class="hljs-keyword">s</span>] <= data[t]) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> B[k] = data[<span class="hljs-keyword">s</span>];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="hljs-keyword">s</span>++;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> } <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">else</span></span> {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> B[k] = data[t];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> t++;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> k++;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span> (<span class="hljs-keyword">s</span> == <span class="hljs-keyword">q</span> + <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> B[k++] = data[t++];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">else</span></span> <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> B[k++] = data[<span class="hljs-keyword">s</span>++];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> i = p; i <= r; i++)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> data[i] = B[i];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">}</code></pre><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(171, 25, 66);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">8.基数排序</span></strong></span></p><blockquote class="" data-type="2" data-url="" data-author-name="" data-content-utf8-length="16" data-source-title="" style="color: rgba(0, 0, 0, 0.5);max-width: 100%;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><section class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;">用于大量数,很长的数进行排序时。</p></section></blockquote><ol class="list-paddingleft-2" style=""><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将所有的数的个位数取出,按照个位数进行排序,构成一个序列。</span></p></li><li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">将新构成的所有的数的十位数取出,按照十位数进行排序,构成一个序列。</span></p></li></ol><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><img class="rich_pages " data-ratio="0.7870182555780934" data-s="300,640" data-type="jpeg" data-w="493" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/eZzl4LXykQyU3JG7ZIqlc7Da4r92CygtkT5at46IV0VG1v4OaibB0Hu5XKBiaEAiaIcXUVyiaz5EniasC87XicK41CuQ/640?wx_fmt=jpeg" style="box-sizing: border-box !important; overflow-wrap: break-word !important; visibility: visible !important; width: 493px !important; height: auto !important;" _width="493px" src="https://img2018.cnblogs.com/blog/1112483/201910/1112483-20191007132859348-1264180054.png" crossorigin="anonymous" data-fail="0"></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">代码实现如下:</span></p><p style="max-width: 100%;min-height: 1em;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br></p><pre style="max-width: 100%;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);overflow-x: auto;box-sizing: border-box !important;overflow-wrap: break-word !important;"><code class="hljs perl" style="margin-right: 0.15em;margin-left: 0.15em;padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Menlo, Courier, monospace;overflow: auto;color: rgb(171, 178, 191);text-size-adjust: none;min-width: 400px;background: none 0% 0% / auto repeat scroll padding-box border-box rgb(40, 44, 52);box-sizing: border-box !important;overflow-wrap: break-word !important;display: block !important;"><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">public</span> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">void</span> <span class="" style="max-width: 100%;color: rgb(97, 174, 238);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">sort</span></span><span class="" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;">(<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span>[] <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">array</span>)</span> </span>{<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>首先确定排序的趟数;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> max = <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">array</span>[<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> i = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>; i < <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">array</span>.length; i++) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">if</span></span> (<span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">array</span>[i] > max) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> max = <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">array</span>[i];<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> <span class="hljs-keyword">time</span> = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>判断位数;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">while</span></span> (max > <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> max /= <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">10</span></span>;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="hljs-keyword">time</span>++;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//建立<span class="hljs-number">10</span>个队列;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> List<ArrayList> <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">queue</span> = <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">new</span> ArrayList<ArrayList>();<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> i = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>; i < <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">10</span></span>; i++) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> ArrayList<Integer> queue1 = <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;">new</span> ArrayList<Integer>();<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">queue</span>.add(queue1);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;">//进行<span class="hljs-keyword">time</span>次分配和收集;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> i = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>; i < <span class="hljs-keyword">time</span>; i++) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>分配数组元素;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> j = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>; j < <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">array</span>.length; j++) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>得到数字的第<span class="hljs-keyword">time</span>+<span class="hljs-number">1</span>位数;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> <span class="hljs-keyword">x</span> = <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">array</span>[j] % (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span>) Math.<span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">pow</span>(<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">10</span></span>, i + <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">1</span></span>) / (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span>) Math.<span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">pow</span>(<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">10</span></span>, i);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> ArrayList<Integer> queue2 = <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">queue</span>.get(<span class="hljs-keyword">x</span>);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> queue2.add(<span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">array</span>[j]);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">queue</span>.<span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">set</span>(<span class="hljs-keyword">x</span>, queue2);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> count = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>;<span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>元素计数器;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(92, 99, 112);font-style: italic;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-regexp">//</span>收集队列元素;</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">for</span></span> (<span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">int</span></span> k = <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>; k < <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">10</span></span>; k++) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(198, 120, 221);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-keyword">while</span></span> (<span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">queue</span>.get(k).size() > <span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>) {<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> ArrayList<Integer> queue3 = <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">queue</span>.get(k);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span class="" style="max-width: 100%;color: rgb(230, 192, 123);box-sizing: border-box !important;overflow-wrap: break-word !important;">array</span>[count] = queue3.get(<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> queue3.remove(<span class="" style="max-width: 100%;color: rgb(209, 154, 102);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="hljs-number">0</span></span>);<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> count++;<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"> }</code></pre><pre style="max-width: 100%;letter-spacing: 0.544px;font-size: 15px;color: rgb(62, 62, 62);line-height: inherit;text-align: start;box-sizing: border-box !important;overflow-wrap: break-word !important;"></pre>
</div>