您的位置:首页 > 运维架构 > Shell

Java排序算法优化--Shell排序【温故而知新】

2014-04-21 11:56 274 查看
/**
*
* @author Fly
*/
public class ShellSort {

public int[] shellsort(int[] a) {
int size = a.length;
for (int step = size / 2; step > 0; step /= 2) {
for (int i = 0; i < step; i++) {
for (int j = i; j < size; j += step) {
int temp = a[j], k;
for (k = j; k > i && a[k - step] > temp; k -= step) {
a[k] = a[k - step];
}
a[k] = temp;
}
}
}
return a;
}

public void printArray(int[] a) {
for (int i : a) {
System.out.print(i + ",");
}
System.out.println();
}

public static void main(String[] args) {
int[] a = {2, 3, 1, 5, 7, 8, 9, 0, 11, 10, 12, 13, 14, 4, 6};
ShellSort shellsort = new ShellSort();
shellsort.printArray(a);
shellsort.printArray(shellsort.shellsort(a));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: