java希尔排序

直接贴代码:

package die;

import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Shellsort {

    public static void shellSortSmallToBig(int[] data) {
        int j = 0;
        int temp = 0;
        for (int increment = data.length / 2; increment > 0; increment /= 2) {
              System.out.println("increment:" + increment);
            for (int i = increment; i < data.length; i++) {
              
                temp = data[i];
                for (j = i - increment; j >= 0; j -= increment) {
                    if (temp < data[j]) {
                        data[j + increment] = data[j];
                    } else {
                        break;
                    }
                }
                data[j + increment] = temp;
            }
            for (int i = 0; i < data.length; i++)
                System.out.print(data[i] + " ");
            System.out.println("");
        }
    }
    public static void main(String[] args) throws IOException
    {
        Scanner scanner=new Scanner(System.in); 
        System.out.println("输入数组长度");
        int n=scanner.nextInt();
        //    System.out.println(n);
        System.out.println("输入一个数组");
        Shellsort AK=null;
        int A[]=new int [n];
        for(int i=0;i<n;i++)//这一步之前都是把数组输入
        {A[i]=scanner.nextInt();}
        AK.shellSortSmallToBig(A);
        
    }
}

 

结果截图:

 

posted @ 2021-09-14 10:24  睡觉不困  阅读(41)  评论(0编辑  收藏  举报