@XQF
2018-03-07T22:55:00.000000Z
字数 512
阅读 780
数据结构与算法
主要是这个按照某种间隔进行分组,以插入排序为基础。
gap为1的时候的就等同于插入排序
public class Solution {
public int[] shellSort(int[] nums) {
int len = nums.length;
//gap是不能为0的,注意
for (int gap = len / 2; gap > 0; gap /= 2) {
// 对所有元素插入排序,间隔为gap
for (int i = gap; i < len; i++) {
int temp = nums[i];
int j;
for (j = i - gap; j >= 0; j -= gap) {
if (temp < nums[j]) {
nums[j + gap] = nums[j];
} else {
break;
}
}
nums[gap + j] = temp;
}
}
return nums;
}
public static void main(String[] args) {
int[] nums = {1, 2, 55, 6, 3, 2, 4, 5, 6, 8, 9, 0};
Solution solution = new Solution();
System.out.println(Arrays.toString(solution.shellSort(nums)));
}
}