99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀各種排序算法總結
各種排序算法總結
2016-04-03
收藏

各種排序算法總結

排序算法是最基本最常用的算法,不同的排序算法在不同的場景或應用中會有不同的表現(xiàn),我們需要對各種排序算法熟練才能將它們應用到實際當中,才能更好地發(fā)揮它們的優(yōu)勢。今天,來總結下各種排序算法。

下面這個表格總結了各種排序算法的復雜度與穩(wěn)定性:

QQ截圖20160401095559.png


各種排序算法復雜度比較.png

冒泡排序

冒泡排序可謂是最經(jīng)典的排序算法了,它是基于比較的排序算法,時間復雜度為O(n^2),其優(yōu)點是實現(xiàn)簡單,n較小時性能較好。

算法原理

相鄰的數(shù)據(jù)進行兩兩比較,小數(shù)放在前面,大數(shù)放在后面,這樣一趟下來,最小的數(shù)就被排在了第一位,第二趟也是如此,如此類推,直到所有的數(shù)據(jù)排序完成

c++代碼實現(xiàn)

void bubble_sort(int arr[], int len)

{

      for (int i = 0; i < len - 1; i++)

      {

          for (int j = len - 1; j >= i; j--)

          {

              if (arr[j] < arr[j - 1])

              {

                  int temp = arr[j];

                  arr[j] = arr[j - 1];

                  arr[j - 1] = temp;

              }

          }

      }

}

選擇排序

算法原理

先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再從剩余未排序元素中繼續(xù)尋找最?。ù螅┰?,然后放到已排序序列的末尾。以此類推,直到所有元素均排序完畢。

c++代碼實現(xiàn)

void select_sort(int arr[], int len)

  {

      for (int i = 0; i < len; i++)

      {

          int index = i;

          for (int j = i + 1; j < len; j++)

          {

              if (arr[j] < arr[index])

                  index = j;

          }

          if (index != i)

          {

              int temp = arr[i];

              arr[i] = arr[index];

              arr[index] = temp; 

          }

      }

  }



插入排序

算法原理

將數(shù)據(jù)分為兩部分,有序部分與無序部分,一開始有序部分包含第1個元素,依次將無序的元素插入到有序部分,直到所有元素有序。插入排序又分為直接插入排序、二分插入排序、鏈表插入等,這里只討論直接插入排序。它是穩(wěn)定的排序算法,時間復雜度為O(n^2)

c++代碼實現(xiàn)

void insert_sort(int arr[], int len)

  {

      for (int i = 1; i < len; i ++)

      {

          int j = i - 1;

          int k = arr[i];

          while (j > -1 && k < arr[j] )

          {

              arr[j + 1] = arr[j];

              j --;

          }

          arr[j + 1] = k;

      }

  }

快速排序

算法原理

快速排序是目前在實踐中非常高效的一種排序算法,它不是穩(wěn)定的排序算法,平均時間復雜度為O(nlogn),最差情況下復雜度為O(n^2)。它的基本思想是:通過一趟排序將要排序的數(shù)據(jù)分割成獨立的兩部分,其中一部分的所有數(shù)據(jù)都比另外一部分的所有數(shù)據(jù)都要小,然后再按此方法對這兩部分數(shù)據(jù)分別進行快速排序,整個排序過程可以遞歸進行,以此達到整個數(shù)據(jù)變成有序序列。

c++代碼實現(xiàn)

void quick_sort(int arr[], int left, int right)

{

  if (left < right)

  {

      int i = left, j = right, target = arr[left];

      while (i < j)

      {

          while (i < j && arr[j] > target)

              j--;

          if (i < j)

              arr[i++] = arr[j];


          while (i < j && arr[i] < target)

              i++;

          if (i < j)

              arr[j] = arr[i];

      }

      arr[i] = target;

      quick_sort(arr, left, i - 1);

      quick_sort(arr, i + 1, right);

  }

}

歸并排序

算法原理

歸并排序具體工作原理如下(假設序列共有n個元素):

    將序列每相鄰兩個數(shù)字進行歸并操作(merge),形成floor(n/2)個序列,排序后每個序列包含兩個元素

    將上述序列再次歸并,形成floor(n/4)個序列,每個序列包含四個元素

    重復步驟2,直到所有元素排序完畢

    歸并排序是穩(wěn)定的排序算法,其時間復雜度為O(nlogn),如果是使用鏈表的實現(xiàn)的話,空間復雜度可以達到O(1),但如果是使用數(shù)組來存儲數(shù)據(jù)的話,在歸并的過程中,需要臨時空間來存儲歸并好的數(shù)據(jù),所以空間復雜度為O(n)

    c++代碼實現(xiàn)

void merge(int arr[], int temp_arr[], int start_index, int mid_index, int end_index)

  {

      int i = start_index, j = mid_index + 1;

      int k = 0;

      while (i < mid_index + 1 && j < end_index + 1)

      {

          if (arr[i] > arr[j])

              temp_arr[k++] = arr[j++];

          else

              temp_arr[k++] = arr[i++];

      }

      while (i < mid_index + 1)

      {

          temp_arr[k++] = arr[i++];

      }

      while (j < end_index + 1)

          temp_arr[k++] = arr[j++];


      for (i = 0, j = start_index; j < end_index + 1; i ++, j ++)

          arr[j] = temp_arr[i];

  }


  void merge_sort(int arr[], int temp_arr[], int start_index, int end_index)

  {

      if (start_index < end_index)

      {

          int mid_index = (start_index + end_index) / 2;

          merge_sort(arr, temp_arr, start_index, mid_index);

          merge_sort(arr, temp_arr, mid_index + 1, end_index);

          merge(arr, temp_arr, start_index, mid_index, end_index);

      }

  }

堆排序

二叉堆

二叉堆是完全二叉樹或者近似完全二叉樹,滿足兩個特性

父結點的鍵值總是大于或等于(小于或等于)任何一個子節(jié)點的鍵值

每個結點的左子樹和右子樹都是一個二叉堆

當父結點的鍵值總是大于或等于任何一個子節(jié)點的鍵值時為最大堆。當父結點的鍵值總是小于或等于任何一個子節(jié)點的鍵值時為最小堆。一般二叉樹簡稱為堆。

堆的存儲

一般都是數(shù)組來存儲堆,i結點的父結點下標就為(i – 1) / 2。它的左右子結點下標分別為2 * i + 1和2 * i + 2。如第0個結點左右子結點下標分別為1和2。存儲結構如圖所示:


QQ截圖20160401095613.png

堆結構.png


堆排序原理

堆排序的時間復雜度為O(nlogn)

    算法原理(以最大堆為例)

    先將初始數(shù)據(jù)R[1..n]建成一個最大堆,此堆為初始的無序區(qū)

    再將關鍵字最大的記錄R[1](即堆頂)和無序區(qū)的最后一個記錄R[n]交換,由此得到新的無序區(qū)R[1..n-1]和有序區(qū)R[n],且滿足R[1..n-1].keys≤R[n].key

    由于交換后新的根R[1]可能違反堆性質,故應將當前無序區(qū)R[1..n-1]調整為堆。

    重復2、3步驟,直到無序區(qū)只有一個元素為止。

c++代碼實現(xiàn)

/**

 * 將數(shù)組arr構建大根堆

 * @param arr 待調整的數(shù)組

 * @param i   待調整的數(shù)組元素的下標

 * @param len 數(shù)組的長度

 */

void heap_adjust(int arr[], int i, int len)

{

    int child;

    int temp;


    for (; 2 * i + 1 < len; i = child)

    {

        child = 2 * i + 1;  // 子結點的位置 = 2 * 父結點的位置 + 1

        // 得到子結點中鍵值較大的結點

        if (child < len - 1 && arr[child + 1] > arr[child])

            child ++;

        // 如果較大的子結點大于父結點那么把較大的子結點往上移動,替換它的父結點

        if (arr[i] < arr[child])

        {

            temp = arr[i];

            arr[i] = arr[child];

            arr[child] = temp;

        }

        else

            break;

    }

}


/**

 * 堆排序算法

 */

void heap_sort(int arr[], int len)

{

    int i;

    // 調整序列的前半部分元素,調整完之后第一個元素是序列的最大的元素

    for (int i = len / 2 - 1; i >= 0; i--)

    {

        heap_adjust(arr, i, len);

    }


    for (i = len - 1; i > 0; i--)

    {

        // 將第1個元素與當前最后一個元素交換,保證當前的最后一個位置的元素都是現(xiàn)在的這個序列中最大的

        int temp = arr[0];

        arr[0] = arr[i];

        arr[i] = temp;

        // 不斷縮小調整heap的范圍,每一次調整完畢保證第一個元素是當前序列的最大值

        heap_adjust(arr, 0, i);

    }

}

其它排序代碼,待補充。。。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }