2018-01-25 06:20

# 希尔排序

/**
*
* Date : 2017/12/9.
*/
public class Solution {
public static void  main(String[] agrs){
int[] a = new int[10];
for (int i = 0; i < 10; i++) {
a[i] = (int) (Math.random() * 100);
}
System.out.println("原数组--------------");
for (int i = 0; i < 10; i++) {
System.out.print(a[i] + ",");
}
System.out.println();
shellSort(a);
System.out.println("");
System.out.println("结果数组---------");
for (int i = 0; i < 10; i++) {
System.out.print(a[i] + ",");
}
}

public static int[] shellSort(int[] a) {
int x = 0;
for (int d = a.length / 2; d > 0; d /= 2) {
for (int j = d; j < a.length; j ++) {
while (j > 0 && a[j] < a[j - d]) {
int temp = a[j];
a[j] = a[j - d];
a[j - d] = temp;
}
}
x++;
System.out.println("第" + x + "次d=" + d);
System.out.println("第" + x + "次排序结果。");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + ",");
}
System.out.println();
}
return a;
}
}

91,48,74,56,84,30,44,90,9,1,

30,44,74,9,1,91,48,90,56,84,

30,9,1,44,48,90,56,84,74,91,

9,1,30,44,48,56,84,74,90,91,

---------
9,1,30,44,48,56,84,74,90,91,
Process finished with exit code 0