数据结构实验之排序六:希尔排序
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem De ion
我们已经学习了各种排序方法,知道在不同的情况下要选择不同的排序算法,以期达到最好的排序效率;对于待排序数据来说,若数据基本有序且记录较少时, 直接插入排序的效率是非常好的,希尔排序就是针对一组基本有序的少量数据记录进行排序的高效算法。你的任务是对于给定的数据进行希尔排序,其中增量dk=n/(2^k)(k=1,2,3……)
Input
连续输入多组数据,每组输入数据的第一行给出一个正整数N(N <= 10000),随后连续给出N个整数表示待排序关键字,数字间以空格分隔。
Output
输出dk=n/2和dk=1时的结果。
Sample Input
10 10 9 8 7 6 5 4 3 2 1 10 -5 9 7 -11 37 -22 99 288 33 66
Sample Output
5 4 3 2 1 10 9 8 7 6 1 2 3 4 5 6 7 8 9 10 -22 9 7 -11 37 -5 99 288 33 66 -22 -11 -5 7 9 33 37 66 99 288
Hint
Source
xam
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n;
int a[10005], b[10005];
void shellsort(int a[], int n, int dk)
{
int i, j, t;
for(i = dk; i < n; i++)
{
for(j = i-dk; j >= 0; j-=dk)
{
if(a[j] > a[j + dk])
{
t = a[j];
a[j] = a[j+dk];
a[j+dk] = t;
}
}
}
}
int main()
{
int i;
while(scanf(\"%d\", &n) != EOF)
{
for(i = 0; i < n; i++)
{
scanf(\"%d\", &a[i]);
b[i] = a[i];
}
shellsort(a, n, n/2);
int i;
for(i = 0; i < n; i++)
{
if(i == n - 1)
{
printf(\"%d\\n\", a[i]);
}
else
{
printf(\"%d \", a[i]);
}
}
shellsort(b,n,1);
for(i = 0; i < n; i++)
{
if(i == n - 1)
{
printf(\"%d\\n\", b[i]);
}
else
{
printf(\"%d \", b[i]);
}
}
}
return 0;
}
继续阅读与本文标签相同的文章
下一篇 :
华讯助力车企优化安全策略统一管理
-
海思向公开市场推出首款4G通信芯片Balong 711
2026-05-18栏目: 教程
-
猫和老鼠:5种药水效果可以叠加吗?这2种药水效果会有冲突!
2026-05-18栏目: 教程
-
自媒体教程,深度剖析平台的推荐机制原理,了解怎么获取高流量
2026-05-18栏目: 教程
-
宽带故障怎么办?教你几招,轻松解决!
2026-05-18栏目: 教程
-
Python 3.8刚刚发布!一分钟了解新版本的强大功能!
2026-05-18栏目: 教程
