【java】希尔排序


import java.util.*;


public class Test {

  public static void main(String[] args){
        int[] arr = new int[]{2,1,6,4,7,9};
       shellsort(arr);
        System.out.println(Arrays.toString(arr));
  }

  public  static void shellsort(int[] nums){
      int n = nums.length;
      int gap = n / 2;
      while(gap > 0){
          for(int j = gap ; j < n ;j ++){
              int i = j;
              while(i >= gap && nums[i - gap] > nums[i]){
                  int temp = nums[i];
                  nums[i] = nums[i - gap];
                  nums[i - gap] = temp;
                  i -= gap;
              }


          }
          gap = gap / 2;
      }
  }

}

你可能感兴趣的:(JAVA)