public class ShellSort {
public static void sort(int[] arr) {
int j;
int step;
int len = arr.length;
for (step = len / 2; step > 0; step /= 2) {
for (int i = 0; i < step; i++) {
for (j = i + step; j < len; j += step) {
if (arr[j] < arr[j - step]) {
int temp = arr[j];
int k = j - step;
while (k >= 0 && arr[k] > temp) {
arr[k + step] = arr[k];
k -= step;
}
arr[k + step] = temp;
}
}
}
}
}
public static void main(String[] args) {
int[] arr = new int[] { 23, 98, 45, 2, 24, 64, 12, 1 };
sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
System.out.print(" ");
}
}
}
网友评论