希尔排序(Shell Sort)又称缩小增量排序算法的基本思路是:先取一个小于n的整数(称为增量)而后把排序中的n个记录分为若干个子表从下标0开始间隔为i的记录组成一个子表在各个子表内进行直接插入排序随着增量的减少达到排序目的。java
算法性能分析:算法
(1) 空间复杂度 希尔排序用到了直接插入排序因此空间复杂度为O(1)shell
(2) 时间复杂度 O(n ^2数组
(3)算法的稳定性 不稳定的排序算法性能
/* * Kiss_My_Love * 2012/8/22 * 希尔排序 **/ public static void shellArray(Object[] a){ for(int i=a.length/2;i>0;i/=2){ //对排序数列设置增量,并肯定外围循环次数 for(int j=0;j<i;j++){ insertSort(a,j,i);//单位数组进行排序 } } } private static void insertSort(Object[] A, int start, int d) { int t; for (int i = start + d; i < A.length; i += d) { for (int j = i; (j >= d) && ((Integer)A[j] <(Integer) A[j - d]); j -= d) { t = (Integer) A[j]; A[j] = A[j - d]; A[j - d] = t; } }
听所还有一个更加犀利的算法比较NB如今和你们一块儿分享 这个是别人写的给你们拿出来好好看看:spa
/* * Kiss_My_Love * 2012/8/23 * 希尔排序 **/ public static Object[] shellSort(Object[] arr){ int i,j,n=1,temp,len = arr.length; while(n<=len/3) n = n*3+1; while(n > 0){ for (i = n; i < len; i++) { temp = (Integer) arr[i]; j = i; while(j >= n && (Integer)arr[j - n] >= temp){ arr[j] = arr[j - n]; j-=n; } arr[j] = temp; } n = (n - 1)/3; } return arr; }