<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x

    冒泡排序與插入排序比較

    發布時間:2016-8-25 10:41    發布者:designapp
    關鍵詞: 冒泡排序 , 插入排序
      同事設計一款產品的軟件系統結束了。但是最后幾天發現系統不能使用,好像是看門狗一直復位。我試著debug一下,發現確實是看門狗復位造成的。在以前同事一直關閉關閉看門狗,在完成所有功能后才打開的看門狗。所以現在才發現看門狗復位。盡量延長看門狗復位時間沒有任何效果。所以肯定是某個函數運行時間太長造成了看門狗復位。在瀏覽程序后我發現他使用了冒泡排序:
      void bubbleSort( int sort[], unsigned char len )
      {
      char i,j;
      int temp;
      len -= 2;
      for( i =len; i>=0; i--)
      {
      for( j =0; j冒泡排序。如果按照最極端的情況,排序數組sort恰好是反向那么關鍵字比較次數為n(n-1)/2。移動次數3n(n-1)/2。所以該算法的時間復雜度應該為n*n。我懷疑是冒泡排序引起的復位后,我屏蔽了該函數運行,產品可以正常運行了。時間比較緊,系統不能做大的修改,那就只好更換排序算法了。于是我建議采用插入排序,問題就解決了。產品很快投產上市了。
      代碼如下:
      void insert_sort(int a[],int n)
      {
      int i,j;
      int temp;
      for ( i=1; i
      {
      temp=a[i ]; //把待排序元素賦給temp,temp在while循環中并不改變,這樣方便比較,并且它是 //要插入的元素
      j=i-1;
      //while循環的作用是將比當前元素大的元素都往后移動一個位置
      while ((j>=0)&& (temp
      a[j+1]=a[j];
      j--; // 順序比較和移動,依次將元素后移動一個位置
      }
      a[j+1]=temp;//元素后移后要插入的位置就空出了,找到該位置插入
      }
      }
      我認為是一位插入排序的算法時間效率優于冒泡排序。最近在翻看《數據結構》發現書中介紹冒泡與插入排序的時間都是n*n,也就是n的平方。難道是冒泡和插入排序效率是一樣的。但是問題為什么解決了,一年多上市銷售也沒有發現問題。我們的細細研究一下。
      排序的最極端情況是逆序,那么就采用逆序來測試一下兩種算法。平臺使用VC6.0。
      #include
      void bubble_sort(int a[], int n);
      void bubble_sort(int a[], int n)
      {
      int i, j, temp;
      for (j = 0; j  a[i + 1])
      {
      temp = a[ i];
      a[i ] = a[i + 1];
      a[i + 1] = temp;
      }
      }
      }
      int main( )
      {
      int i;
      int sort[6]={5,4,3,2,1,0};
      bubble_sort( sort, sizeof( sort)/sizeof(int) );
      for( i =0 ; i


      我們可以在bubble_sort(int a[], int n)添加代碼統計出比較次數和**次數。
      #include
      int COMP_COUNT = 0;
      int SWAP_COUNT = 0;
      void bubble_sort(int a[], int n);
      void bubble_sort(int a[], int n)
      {
      int i, j, temp;
      for (j = 0; j  a[i + 1])
      {
      SWAP_COUNT +=3; //**計數器
      temp = a[i ];
      a[i ] = a[i + 1];
      a[i + 1] = temp;
      }
      }
      }
                                    
                                                                   
                                    
                      int main( )
      {
      int i;
      int sort[6]={5,4,3,2,1,0};
      COMP_COUNT = 0;
      SWAP_COUNT = 0;
      bble_sort( sort, sizeof( sort)/sizeof(int) );
      for( i =0 ; i


      使用冒泡比較次數是15,**次數45。
      當然也可以采用同樣辦法獲得插入排序比較次數和**次數。代碼如下:
      #include
      int COMP_COUNT = 0;
      int SWAP_COUNT = 0;
      void insert_sort(int a[],int n);void insert_sort(int a[],int n)
      {
      int i,j;
      int temp;
      for ( i=1; i
      {
      SWAP_COUNT++;
      temp=a[i ]; //把待排序元素賦給temp,temp在while循環中并不改變,這樣方便比較,并且它是 //要插入的元素
      j=i-1;
      //while循環的作用是將比當前元素大的元素都往后移動一個位置
      while ((j>=0)&& (temp
      SWAP_COUNT++;
      COMP_COUNT++;
      a[j+1]=a[j];
      j--; // 順序比較和移動,依次將元素后移動一個位置
      }
      SWAP_COUNT++;
      a[j+1]=temp;//元素后移后要插入的位置就空出了,找到該位置插入
      }
      }
      int main( )
      {
      int i;
      int sort[6]={5,4,3,2,1,0};
      COMP_COUNT = 0;
      SWAP_COUNT = 0;
      insert_sort( sort, sizeof( sort)/sizeof(int) );
      for( i =0 ; i


      使用插入比較次數是25,**次數15。
      冒泡比較次數是15,**次數45。所以盡管資料介紹他們時間復雜度都是n的平方。
      但是在6個元素時插入排序明顯優于冒泡。
      我們可以做一個測試,在1K元算會怎樣?我們可以設計一個完全逆序的數組,元素數量1000,值從1000-1。首先測試插入排序。
      代碼如下:
      #include
      int COMP_COUNT = 0;
      int SWAP_COUNT = 0;
      void bubble_sort(int a[], int n);
      void insert_sort(int a[],int n);
      void insert_sort(int a[],int n)
      {
      int i,j;
      int temp;
      for ( i=1; i
      {
      SWAP_COUNT++;
      temp=a[i ]; //把待排序元素賦給temp,temp在while循環中并不改變,這樣方便比較,并且它是要插入的元素
      j=i-1;
      //while循環的作用是將比當前元素大的元素都往后移動一個位置
      while ((j>=0)&& (temp
      SWAP_COUNT++;
      COMP_COUNT++;
      a[j+1]=a[j];
      j--; // 順序比較和移動,依次將元素后移動一個位置
      }
      SWAP_COUNT++;
      a[j+1]=temp;//元素后移后要插入的位置就空出了,找到該位置插入
      }
      }
      void bubble_sort(int a[], int n)
      {
      int i, j, temp;
      for (j = 0; j  a[i + 1])
      {
      SWAP_COUNT +=3; //**計數器
      temp = a[i ];
      a[i ] = a[i + 1];
      a[i + 1] = temp;
      }
      }
      }
      int main( )
      {
      int i;
      int sort[1000];
      for( i =999 ;i>=0; i--)
      sort[i ] = 999-i;
      COMP_COUNT = 0;
      SWAP_COUNT = 0;
      insert_sort( sort, sizeof( sort)/sizeof(int) );
      //bble_sort( sort, sizeof( sort)/sizeof(int) );
      for( i =0 ; i


      接下來測試插入排序。代碼如下:
      #include
      int COMP_COUNT = 0;
      int SWAP_COUNT = 0;
      void bubble_sort(int a[], int n);
      void insert_sort(int a[],int n);
      void insert_sort(int a[],int n)
      {
      int i,j;
      int temp;
      for ( i=1; i
      {
      SWAP_COUNT++;
      temp=a[ i]; //把待排序元素賦給temp,temp在while循環中并不改變,這樣方便比較,并且它是 //要插入的元素
      j=i-1;
      //while循環的作用是將比當前元素大的元素都往后移動一個位置
      while ((j>=0)&& (temp
      SWAP_COUNT++;
      COMP_COUNT++;
      a[j+1]=a[j];
      j--; // 順序比較和移動,依次將元素后移動一個位置
      }
      SWAP_COUNT++;
      a[j+1]=temp;//元素后移后要插入的位置就空出了,找到該位置插入
      }
      }
      void bubble_sort(int a[], int n)
      {
      int i, j, temp;
      for (j = 0; j  a[i + 1])
      {
      SWAP_COUNT +=3; //**計數器
      temp = a[ i];
      a[i ] = a[i + 1];
      a[i + 1] = temp;
      }
      }
      }
      int main( )
      {
      int i;
      int sort[1000];
      for( i =999 ;i>=0; i--)
      sort[i ] = 999-i;
      COMP_COUNT = 0;
      SWAP_COUNT = 0;
      //insert_sort( sort, sizeof( sort)/sizeof(int) );
      bubble_sort( sort, sizeof( sort)/sizeof(int) );
      for( i =0 ; i


      冒泡**次數1498500,比較次數499500。插入**次數501498,比較次數499500。
      比較次數相同。**次數冒泡次數很多。是插入排序的2.99倍。所以插入排序優于冒泡。
    本文地址:http://www.portaltwn.com/thread-172538-1-1.html     【打印本頁】

    本站部分文章為轉載或網友發布,目的在于傳遞和分享信息,并不代表本網贊同其觀點和對其真實性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問題,我們將根據著作權人的要求,第一時間更正或刪除。
    您需要登錄后才可以發表評論 登錄 | 立即注冊

    廠商推薦

    • Microchip視頻專區
    • EtherCAT®和Microchip LAN925x從站控制器介紹培訓教程
    • MPLAB®模擬設計器——在線電源解決方案,加速設計
    • 讓您的模擬設計靈感,化為觸手可及的現實
    • 深度體驗Microchip自動輔助駕駛應用方案——2025巡展開啟報名!
    • 貿澤電子(Mouser)專區
    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷