標籤:
希爾排序的誕生是由於插入排序在處理大規模數組的時候會遇到需要移動太多元素的問題。希爾排序的思想是將一個大的數組“分而治之”,劃分為若干個小的數組,以 gap 來劃分,比如數組 [1, 2, 3, 4, 5, 6, 7, 8] ,如果以 gap = 2 來劃分,可以分為 [1, 3, 5, 7] 和 [2, 4, 6, 8] 兩個數組(對應的,如 gap = 3 ,則劃分的數組為: [1, 4, 7] 、 [2, 5, 8] 、 [3, 6] )然後分別對劃分出來的數組進行插入排序,待各個子數組排序完畢之後再減小 gap 值重複進行之前的步驟,直至 gap = 1 ,即對整個數組進行插入排序,此時的數組已經基本上快排好序了,所以需要移動的元素會很小很小,解決了插入排序在處理大規模數組時較多移動次數的問題。
具體執行個體請參照插入排序。
希爾排序是插入排序的改進版,在資料量大的時候對效率的提升協助很大,資料量小的時候建議直接使用插入排序就好了。
實現代碼:
public <T extends Comparable<T>> void sort(T[] array, boolean ascend) { int length = array.length; int gap = 1; while (gap < length / 3) { gap = gap * 3 + 1; } while (gap >= 1) { for (int i = gap; i < length; i++) { T next = array[i]; int j = i; while (j >= gap) { int compare = array[j - gap].compareTo(next); if (compare == 0 || compare < 0 == ascend) { break; } array[j] = array[j - gap]; j -= gap; } if (j != i) { array[j] = next; } } gap /= 3; } }
例2:
package org.rut.util.algorithm.support;import org.rut.util.algorithm.SortUtil;public class ShellSort implements SortUtil.Sort{ /* (non-Javadoc) * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ public void sort(int[] data) { for(int i=data.length/2;i>2;i/=2){ for(int j=0;j<i;j++){ insertSort(data,j,i); } } insertSort(data,0,1); } /** * @param data * @param j * @param i */ private void insertSort(int[] data, int start, int inc) { int temp; for(int i=start+inc;i<data.length;i+=inc){ for(int j=i;(j>=inc)&&(data[j]<data[j-inc]);j-=inc){ SortUtil.swap(data,j,j-inc); } } }}
SortUtil:package org.rut.util.algorithm;import org.rut.util.algorithm.support.BubbleSort;import org.rut.util.algorithm.support.HeapSort;import org.rut.util.algorithm.support.ImprovedMergeSort;import org.rut.util.algorithm.support.ImprovedQuickSort;import org.rut.util.algorithm.support.InsertSort;import org.rut.util.algorithm.support.MergeSort;import org.rut.util.algorithm.support.QuickSort;import org.rut.util.algorithm.support.SelectionSort;import org.rut.util.algorithm.support.ShellSort;public class SortUtil { public final static int INSERT = 1; public final static int BUBBLE = 2; public final static int SELECTION = 3; public final static int SHELL = 4; public final static int QUICK = 5; public final static int IMPROVED_QUICK = 6; public final static int MERGE = 7; public final static int IMPROVED_MERGE = 8; public final static int HEAP = 9; public static void sort(int[] data) { sort(data, IMPROVED_QUICK); } private static String[] name={ "insert", "bubble", "selection", "shell", "quick", "improved_quick", "merge", "improved_merge", "heap" }; private static Sort[] impl=new Sort[]{ new InsertSort(), new BubbleSort(), new SelectionSort(), new ShellSort(), new QuickSort(), new ImprovedQuickSort(), new MergeSort(), new ImprovedMergeSort(), new HeapSort() }; public static String toString(int algorithm){ return name[algorithm-1]; } public static void sort(int[] data, int algorithm) { impl[algorithm-1].sort(data); } public static interface Sort { public void sort(int[] data); } public static void swap(int[] data, int i, int j) { int temp = data<i>; data<i> = data[j]; data[j] = temp; }}
Java演算法-希爾排序