java shell排序

原理图:



package suanfa;
public class shellInsert { public void shellInsert1(double [] sorted,int inc){ int sorted_length=sorted.length; for(int i=inc+1;i<sorted_length;i++){ if(sorted[i]<sorted[i-inc]){ sorted[0]=sorted[i]; int insertpos=i; for(int k=i-inc;k>=0;k--){ if(sorted[k]>sorted[0]){ sorted[k+inc]=sorted[k]; if(k-inc<0){ insertpos=k; } } else{ insertpos=k+inc; break; } } sorted[insertpos]=sorted[0]; } } } public static void main(String[] args) { // TODO Auto-generated method stub double [] sorted=new double[]{0.0,9.8,2.3,4.5,6.7,1.2,7.5,2.3,4.5,6.7}; int[] incs={7,5,3,1}; shellInsert shell=new shellInsert(); for (int i=0;i<incs.length;i++){ shell.shellInsert1(sorted, incs[i]); } } }
原文地址:https://www.cnblogs.com/luo-mao/p/6030434.html