package com.algorithms;
public class Shell {
/**
* @param intergers
* @return
*/
public static int[] sort(int[] intergers) {
int n=intergers.length;
int h=1;
//根据数组长度确定步长序列
while(h<n/3)
h=3*h+1;
while(h>=1){
for (int i = h; i < n; i++) {
for (int j = i; j >=h&&intergers[j]<intergers[j-h]; j-=h) {
int t=intergers[j];
intergers[j]=intergers[j-h];
intergers[j-h]=t;
}
}
h=h/3;
}
return intergers;
}
public static void main(String[] args) {
int[] integers=sort(new int[]{3,9,5,7,1,4,8});
for (int i : integers) {
System.out.println(i);
}
}
}
希尔排序(java实现)
最新推荐文章于 2023-01-03 16:45:49 发布