对于给定的一组关键值(41,62,13,84,35,96,57,39,79,61,15,83)按希尔...
发布网友
发布时间:2024-03-21 13:31
我来回答
共1个回答
热心网友
时间:2024-07-19 19:35
public class Demo {
public static void main(String[] args) {
int num[] = {5,2,1};
int data[]={41,62,13,84,35,96,57,39,79,61,15,83};
new Demo().ShellSort(num, data);
for (int n:data){
System.out.print(n+" ");
}
}
public void ShellSort(int num[], int data[])
{
for (int k=0; k<num.length; k++)
ShellInsert(data, num[k]);
}
private void ShellInsert(int data[], int dk)
{
for (int i=0; i<dk; i++)
{
//插入排序
for(int j = i+dk; j <data.length ; j +=dk )
{
if( data[j] < data[j -dk])
{
int temp = data[j]; //哨兵
int k = j-dk;
for(; k >=0&& temp < data[k]; k -=dk)
{
data[k+dk] =data[k];
}
data[k+dk] =temp;
}
}
}
}
}
结果:
13 15 35 39 41 57 61 62 79 83 84 96