希尔排序 Java实现

来源:互联网 发布:古装淘宝 编辑:程序博客网 时间:2024/06/05 23:40
package fenshujs;


import java.util.Arrays;
import java.util.Scanner;


public class bishi {

private static void ShellSort(int[] a)
{
int h = 1;
while(h<=a.length/3)
h = h*3+1;
while(h>0)
{
for(int i = 0;i<h;i++)
{
//里面是一个插入排序
for(int inner = i+h;inner<a.length;inner+=h)
{
int temp = a[inner];
while(inner>=h&&temp<a[inner-h])
{
           a[inner] = a[inner-h];
           inner -= h;
       }
a[inner] = temp;
}
}
h = (h-1)/3;
}
}

public static void main(String[] args){
   
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0;i<n;i++)
a[i] = sc.nextInt();
ShellSort(a);
for(int aa:a)
System.out.print(aa+" ");


}


}



10
1 10 2 9 3 7 4 6 5 8
1 2 3 4 5 6 7 8 9 10 


原创粉丝点击