List<Integer> stepSequence =sedgewickStepSequence();for(Integer step : stepSequence){sort(step);}privatevoidsort(int step){for(int col =0; col < step; col++){for(int begin = col + step; begin < array.length; begin += step){int cur = begin;while(cur > col &&cmp(cur, cur - step)<0){swap(cur, cur - step);
cur -= step;}}}}
public List<Integer>sortArray(int[] nums){
List<Integer> stepSequence =shellStepSequence(nums.length);for(Integer step : stepSequence){sort(step, nums);}
List<Integer> array =newArrayList<>();for(int i =0; i < nums.length; i++){
array.add(nums[i]);}return array;}privatevoidsort(int step,int[] nums){// col : 第几列,column简称for(int col =0; col < step; col++){// 对第col列进行排序// col、col+step、col+2*step、col+3*stepfor(int begin = col + step; begin < nums.length; begin += step){int cur = begin;int v = nums[cur];while(cur > col && v < nums[cur - step]){
nums[cur]= nums[cur - step];
cur -= step;}
nums[cur]= v;}}}private List<Integer>shellStepSequence(int length){
List<Integer> stepSequence =newArrayList<>();int step = length;while((step >>=1)>0){
stepSequence.add(step);}return stepSequence;}
最终简洁版
publicint[]sortArray(int[] nums){for(int step = nums.length >>1; step >=1; step >>=1){for(int col =0; col < step; col++){// 对第col列进行排序// col、col+step、col+2*step、col+3*stepfor(int begin = col + step; begin < nums.length; begin += step){int cur = begin;int v = nums[cur];while(cur > col && v < nums[cur - step]){
nums[cur]= nums[cur - step];
cur -= step;}
nums[cur]= v;}}}return nums;}