Broadview 2019-07-27
假如我们现在要排序的数组为[3,1,0,2,8,4,2]。那么选择排序的排序流程为:
现在整个数组是不是已经变得有序了呢。接下来我们看图解版本
接下来上代码<figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">int</span> i; <span class="comment">// 有序区的末尾位置</span></span><br><span class="line"><span class="keyword">int</span> j; <span class="comment">// 无序区的起始位置</span></span><br><span class="line"><span class="keyword">int</span> <span class="built_in">min</span>; <span class="comment">// 无序区中最小元素位置</span></span><br><span class="line"><span class="keyword">int</span> []a=<span class="keyword">new</span> <span class="keyword">int</span>[]{<span class="number">3</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">8</span>,<span class="number">4</span>,<span class="number">2</span>};</span><br><span class="line"></span><br><span class="line"><span class="built_in">for</span>(i=<span class="number">0</span>,n=a.length; i<n; i++) {</span><br><span class="line"> <span class="built_in">min</span>=i;</span><br><span class="line"> <span class="comment">// 找出"a[i+1] ... a[n]"之间的最小元素,并赋值给min。</span></span><br><span class="line"> <span class="built_in">for</span>(j=i+<span class="number">1</span>; j<n; j++) {</span><br><span class="line"> <span class="built_in">if</span>(a[j] < a[<span class="built_in">min</span>])</span><br><span class="line"> <span class="built_in">min</span>=j;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 若min!=i,则交换 a[i] 和 a[min]。</span></span><br><span class="line"> <span class="comment">// 交换之后,保证了a[0] ... a[i] 之间的元素是有序的。</span></span><br><span class="line"> <span class="built_in">if</span>(<span class="built_in">min</span> != i) {</span><br><span class="line"> <span class="keyword">int</span> tmp = a[i];</span><br><span class="line"> a[i] = a[<span class="built_in">min</span>];</span><br><span class="line"> a[<span class="built_in">min</span>] = tmp;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>相信大家都有打扑克的经历,那么我们今天的插入排序就以拿牌为例开始讲(注意只是举例,不是按打牌的规则哦)
现在你明白什么叫做插入排序了么?如果你不明白的话也没关系,我还专门画了一张图:
接下来上代码
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="built_in">int</span> []<span class="built_in">num</span>=<span class="keyword">new</span> <span class="built_in">int</span>[]{<span class="number">3</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">8</span>,<span class="number">4</span>,<span class="number">2</span>};</span><br><span class="line"><span class="keyword">for</span> (<span class="built_in">int</span> i=<span class="number">1</span>,n=<span class="built_in">num</span>.length;i<n;i++){</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">num</span>[i]<<span class="built_in">num</span>[i<span class="number">-1</span>]){</span><br><span class="line"> <span class="keyword">for</span> (<span class="built_in">int</span> j=i;j><span class="number">0</span>;j--){</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">num</span>[j]<<span class="built_in">num</span>[j<span class="number">-1</span>]){</span><br><span class="line"> <span class="built_in">int</span> temp=<span class="built_in">num</span>[j];</span><br><span class="line"> <span class="built_in">num</span>[j]=<span class="built_in">num</span>[j<span class="number">-1</span>];</span><br><span class="line"> <span class="built_in">num</span>[j<span class="number">-1</span>]=temp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="keyword">for</span> (<span class="built_in">int</span> i:<span class="built_in">num</span>){</span><br><span class="line"> System.out.<span class="built_in">print</span>(i+<span class="string">","</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>快速排序是一个运用了分治法和递归算法的排序方式。假如我们现在要排序的数组为[3,1,0,2,8,4,2]。那么在进行快速排序的时候我们先要进行一些准备:
下面开始排序:
接下来就来看一个图片描述的过程
希尔排序呢,其实可以理解为插入算法排序的一个升级版了.
回忆一下当使用插入排序在进行排序数据量非常大的数据时,有一个很小的数据出现在了数组的最后,那么我们就要移动了这个数据前面所有的元素给它放置到合适的元素。例如:我们要排序的数组为[1,2,3,4,5,6,7,。。。此处省略一百万。。.,0]
相信大家肯定不喜欢这个0往前移动一百万此吧,希尔排序的出现其实就是为了解决这个问题的
希尔排序使用了分治算法,先把整个大的数组根据某个增量分为若干个组,先对这若干个组进行一个调整,保证大部分小的数据会被调整到前面来。到最后再次进行插入排序,这样就大大加快了效率了。
来一个例子,如图所示,我们要排序的数组为[3,1,0,2,8,4,2,6,9,1,3,-2,8],
不知道现在的你明白希尔排序了么?来看一看代码吧
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">shellSort</span><span class="params">(<span class="keyword">int</span> <span class="built_in">list</span>[], <span class="keyword">int</span> length)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> gap,i,j,temp;</span><br><span class="line"> <span class="keyword">for</span> (gap = length/<span class="number">2</span>; gap > <span class="number">0</span>; gap /= <span class="number">2</span>){</span><br><span class="line"> <span class="keyword">for</span>(i = gap; i < length; i++){</span><br><span class="line"> <span class="keyword">for</span>(j = i-gap; j>=<span class="number">0</span> && <span class="built_in">list</span>[j]><span class="built_in">list</span>[j+gap]; j -= gap){</span><br><span class="line"> temp = <span class="built_in">list</span>[j];</span><br><span class="line"> <span class="built_in">list</span>[j] = <span class="built_in">list</span>[j+gap];</span><br><span class="line"> <span class="built_in">list</span>[j+gap] = temp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>冒泡排序在排序算法中效率算最慢的一类了,但是因为它简单的缘故仍然是工作1-3年的程序员面试经常会碰到的算法问题。
假如我们现在要排序的数组为[3,1,0,2,8,4,2]
本文源码可见https://github.com/shiyujun/syj-study-demo
要知道时间复杂度只是描述一个增长趋势,复杂度为O的排序算法执行时间不一定比复杂度为O长,因为在计算O时省略了系数、常数、低阶。实际上,在对小规模数据进行排序时,n2的值实际比 knlogn+c还要小。