希尔排序的诞生是由于插入排序在处理大规模数组的时候会遇到需要移动太多元素的问题。希尔排序的思想是将一个大的数组“分而治之”,划分为若干个小的数组,以 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; } }
时间: 2024-09-28 04:57:41