java實現折半排序演算法_java

來源:互聯網
上載者:User

折半插入排序(binary insertion sort)是對插入排序演算法的一種改進,由於排序演算法過程中,就是不斷的依次將元素插入前面已排好序的序列中。由於前半部分為已排好序的數列,這樣我們不用按順序依次尋找插入點,可以採用折半尋找的方法來加快尋找插入點的速度。

public static void halfSort(int[] array) {    int low, high, mid;    int tmp, j;    for (int i = 1; i < array.length; i++) {      tmp = array[i];      low = 0;      high = i - 1;      while (low <= high) {        mid = low + (high - low) / 2;        if (array[mid] > tmp)          high = mid - 1;        else          low = mid + 1;      }      for (j = i - 1; j > high; j--) {        array[j + 1] = array[j];      }      array[high + 1] = tmp;    }  }

折半排序演算法示意圖:

以上所述就是本文的全部內容了,希望能夠對大家學習java折半排序演算法有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.