用Java實現希爾排序

來源:互聯網
上載者:User
package ch10;public class ShellInsertSort {/** * 希爾排序 * @param <T> * @param t:待排序數組 * @param dlta:增量數組 * @return */public static <T extends Comparable> boolean shellSort(T[] t, int[] dlta){if(t==null || t.length<=1) return true;for(int i = 0; i<dlta.length; i++){if(!shellInsert(t, dlta[i])){return false;}}return true;}/** * 一趟希爾排序 * @param <T> * @param t:待排序數組 * @param dk:增量 * @return */private static <T extends Comparable> boolean shellInsert(T[] t, int dk){if(t==null || t.length <= dk) return true;for(int i = dk; i<=t.length-1 ;i++){if(t[i].compareTo(t[i-dk]) < 0 ){T temp = t[i];int j;for(j = i-dk; j>=0 && temp.compareTo(t[j])<0; j-=dk){t[j+dk] = t[j];}t[j+dk] = temp;}}return true;}public static void main(String[] args) {Integer[] arr = new Integer[]{3,5,2,1,7,9,8,1,1,1,1};int[] dlta = new int[]{5,3,1};ShellInsertSort.shellSort(arr, dlta);for(int i : arr){System.out.println(i);}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.