Java排序演算法(五)--希爾排序(ShellSort)
阿新 • • 發佈:2018-12-24
希爾排序(插入排序-漸減增量排序diminishing increment sort):
思想:1.將原始陣列按照增量分解為多個數組,分別按插入排序調好子序列的順序;
2. 減小增量,重複第一步,直至增量為1。
public static int [] shellSort (int []d ){
int h =1;
int next =0;
int temp =0;
for(h =1;3*h +1<d .length;){
h=3* h+1 ;
}
for(;h >0;h =(h -1)/3){ //漸減增量,獲得增量值
for(int j =0;j <h ;j ++){ //依次取得增量序列
for(next =h +j ;next <d .length;next +=h ){
//取得子序列
temp= d[ next];
int k =0;
for(k =next ;k >=h &&temp<d[k-h];k-=h){
// 在子系列內進行比較調整
d[ k]= d[ k- h];
}
d[ k]= temp;
}
}
}
return d ;
}