0
点赞
收藏
分享

微信扫一扫

希尔排序法

登高且赋 2022-03-14 阅读 68

代码演示:

package com.piao.sort;

import java.util.Arrays;

/**
 * 希尔排序:
 * 思路:跨维度比较,通过获取其长度来判断维度的值,并慢慢缩小维度(i = n/2,n/4,n/8,......4,2,1)
 */
public class test_shellSort {
    public static void main(String[] args) {
        int[] arr={8,4,5,7,1,3,6,2,9};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void sort(int[] arr){
        int len=arr.length-1;
        Math.floor(len/2);
        for (int i = len/2; i >0; i=i/2) {
            for (int j = 0; j <=len-i; j++) {
                if (arr[j]>arr[j+i]){
                    int temp=arr[j+i];
                    arr[j+i]=arr[j];
                    arr[j]=temp;
                }
            }
        }
    }
}

举报

相关推荐

0 条评论