hh

<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
                                            <div id="content_views" class="markdown_views prism-atom-one-dark">
                            <!-- flowchart 箭頭圖標 勿刪 -->
                            <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
                            <p>我們先來看看冒泡排序代碼:</p>
<pre class="prettyprint"><code class="has-numbering">// 冒泡排序,a 表示數組,n 表示數組大小
public void bubbleSort(int[] a, int n) {
if(n&lt;=1) return;

for(int i=n-1;i&gt;0;i--){
   int flag=false; // 提前退出冒泡循環的標誌位
   for(int j=1;j&lt;=i;j++){
    if(a[j]&lt;a[j-1]){ 
        int temp=a[j];
        a[j]=a[j-1];
        a[j-1]=temp;  // 交換
        flag=true;
}
}
  if(!flag) break; // 沒有數據交換,提前退出
  }
  }
</code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li></ul></pre>
<p>再來看看插入排序算法:</p>
<pre class="prettyprint"><code class="has-numbering">// 插入排序,a 表示數組,n 表示數組大小
public void insertionSort(int[] a, int n) {
  if (n &lt;= 1) return;

  for(int i=1;i&lt;n;i++)  {
    int value=a[i];
    int j=i-1;    // 查找插入的位置
    for(;j&gt;=0;j--){
      if (a[j]&gt;value) {
         a[j+1]=a[j]// 數據移動
      } else {
       break;
      }
    }
   a[j+1]=value;// 插入數據
  }
}

</code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li></ul></pre>
<p>從兩段代碼上看,冒泡排序的數據交換比插入排序的數據移動要複雜,冒泡排序需要三次賦值操作,而插入排序只需要一次。如下:</p>
<pre class="prettyprint"><code class="has-numbering">冒泡排序中數據的交換操作:
if (a[j] &gt; a[j+1]) { // 交換
   int tmp = a[j];
   a[j] = a[j+1];
   a[j+1] = tmp;
   flag = true;
}

插入排序中數據的移動操作:
if (a[j] &gt; value) {
  a[j+1] = a[j];  // 數據移動
} else {
  break;
}

</code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li></ul></pre>
<p>因此,我們對逆序度爲K的數組進行排序,用冒泡排序需要進行3*K次單元時間,而插入排序只需要K次單元時間。因此插入排序性能更優。</p>

            </div>
                        <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-2011a91181.css" rel="stylesheet">
                </div>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章