您的位置:首页 > 其它

希尔排序

2016-04-24 12:55 337 查看
package sun;

public class Sort {
/**
*
* @param 待排序数组

*
*/
public static void shell(int[] a, int l, int r) {
int h;
for (h = l; h <= (r - l) / 9; h = 3 * h + 1);
for (; h > 0; h /= 3) {
for (int i = l + h; i <= r; i++) {
int j = i;
int v = a[i];
while (j >= l + h && v < a[j - h]) {
a[j] = a[j - h];
j -= h;
}
a[j] = v;
}
}

}

public static void main(String[] args) {
int[] arr = { 49, 38, 65, 97, 76, 13, 27, 49 };
shell(arr, 0, arr.length - 1);
for (int a : arr)
System.out.println(a);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: