Java教程

java希尔排序

本文主要是介绍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);
        
    }
}

 

结果截图:

 

这篇关于java希尔排序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!