参考:https://www.cnblogs.com/chengxiao/p/6104371.html
题目描述
给出一个数据序列,使用希尔排序算法进行从小到大的排序。
间隔gap使用序列长度循环除2直到1。
输入
第一行输入t,表示有t个测试示例
第二行输入n,表示第一个示例有n个数据
第三行输入n个数据,都是正整数,数据之间用空格隔开
以此类推
输出
对每个示例,输出希尔排序的每一趟排序结果
样例输入
2
6
111 22 6 444 333 55
8
77 555 33 1 444 77 666 2222
样例输出
6 111 22 6 444 333 55
6 6 22 55 111 333 444
8 77 77 33 1 444 555 666 2222
8 33 1 77 77 444 555 666 2222
8 1 33 77 77 444 555 666 2222
#include<iostream>
using namespace std;
void display(int *a,int n)
{
int i;
cout<<n<<" ";
for(i=0;i<n;i++)
{
cout<<a[i];
if(i<=n-2)
cout<<" ";
}
cout<<endl;
}
void ShellSort(int *a,int low,int high)
{
int temp;int i,j;
int gap=high-low+1;
for(gap=gap/2;gap>=1;gap=gap/2)
{
for(i=low+gap;i<high;i=i++)//直接插入排序
{
if(a[i]<a[i-gap])
{
temp=a[i]; j=i-gap;
do{
a[j+gap]=a[j];
j=j-gap;
}while(j>=low&&a[j]>temp);
a[j+gap]=temp;
}
}
display(a,high);
}
}
int main()
{
int n;int i;int t;
cin>>t;
while(t--)
{
cin>>n;
int *a=new int[n];
for(i=0;i<n;i++)
cin>>a[i];
ShellSort(a,0,n);
delete []a;
}
return 1;
}