时间限制:1000MS 代码长度限制:10KB
提交次数:1858 通过次数:1304
题型: 编程题 语言: G++;GCC
Description
用函数实现希尔(shell)排序,并输出每趟排序的结果,初始增量d=n/2,其后d=d/2
输入格式
第一行:键盘输入待排序关键的个数n 第二行:输入n个待排序关键字,用空格分隔数据
输出格式
每行输出一趟排序结果,数据之间用一个空格分隔
输入样例
10 5 4 8 0 9 3 2 6 7 1
输出样例
3 2 6 0 1 5 4 8 7 9 1 0 3 2 4 5 6 8 7 9 0 1 2 3 4 5 6 7 8 9
提示
作者
yqm
Version:
0
//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int ans[1000];
void print(int* a)
{
for (int i = 1; i <= a[0]; i++)
cout << a[i] << ' ';
cout << endl;
}
void insertsort(int* ans)
{
int d, i, j, key;
for (d = ans[0] / 2; d > 0; d = d / 2) {
for (i = d + 1; i <= ans[0]; i++) {
if (ans[i] < ans[i - d]) {
key = ans[i];
for (j = i - d; j > 0 && key < ans[j]; j -= d)
ans[j + d] = ans[j];
ans[j + d] = key;
}
}
print(ans);
}
}
int main()
{
int i, n;
cin >> n;
ans[0] = n;
for (i = 1; i <= n; i++)
cin >> ans[i];
insertsort(ans);
}