博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Aizu - ALDS1_2_D Shell Sort 希尔排序
阅读量:3904 次
发布时间:2019-05-23

本文共 2200 字,大约阅读时间需要 7 分钟。

Shell Sort is a generalization of to arrange a list of nn elements AA .

1  insertionSort(A, n, g)2      for i = g to n-13          v = A[i]4          j = i - g5          while j >= 0 && A[j] > v6              A[j+g] = A[j]7              j = j - g8              cnt++9          A[j+g] = v1011 shellSort(A, n)12     cnt = 013     m = ?14     G[] = {?, ?,..., ?}15     for i = 0 to m-116         insertionSort(A, n, G[i])

A function shellSort(A, n) performs a function insertionSort(A, n, g), which considers every gg -th elements. Beginning with large values of gg , it repeats the insertion sort with smaller gg .

Your task is to complete the above program by filling ?. Write a program which reads an integer nn and a sequence AA , and prints mm , Gi(i=0,1,...,m−1)Gi(i=0,1,...,m−1) in the pseudo code and the sequence AA in ascending order. The output of your program must meet the following requirements:

  • 1≤m≤1001≤m≤100
  • 0≤Gi≤n0≤Gi≤n
  • cnt does not exceed ⌈n1.5⌉⌈n1.5⌉

Input

In the first line, an integer nn is given. In the following nn lines, Ai(i=0,1,...,n−1)Ai(i=0,1,...,n−1) are given for each line.

Output

In the first line, print an integer mm . In the second line, print mm integers Gi(i=0,1,...,m−1)Gi(i=0,1,...,m−1) separated by single space character in a line.

In the third line, print cnt in a line. In the following nn lines, print Ai(i=0,1,...,n−1)Ai(i=0,1,...,n−1) respectively.

This problem has multiple solutions and the judge will be performed by a special validator.

Constraints

  • 1≤n≤1,000,0001≤n≤1,000,000
  • 0≤Ai≤1090≤Ai≤109

Sample Input 1

551432

Sample Output 1

24 1312345

Sample Input 2

3321

Sample Output 2

113123

代码如下:

#include 
#include
#include
#include
#include
using namespace std;const int maxn=1000005;int n;int a[maxn];int cnt;vector
G;void InsertSort (int g){ for (int i=g;i
=0&&a[loc]>temp) { a[loc+g]=a[loc]; loc-=g; cnt++; } a[loc+g]=temp; }}void ShellSort (){ int h=1; while (h<=n) { G.push_back(h); h=3*h+1; } for (int i=G.size()-1;i>=0;i--) { InsertSort(G[i]); }}int main(){ scanf("%d",&n); for (int i=0;i
=0;i--) printf("%d%c",G[i],i==0? '\n':' '); printf("%d\n",cnt); for (int i=0;i

 

 

转载地址:http://tpaen.baihongyu.com/

你可能感兴趣的文章
android简单demo学习系例之排版(LinearLayout)[xml-based]
查看>>
J2ME相关的开源项目
查看>>
android简单demo学习系例之排版(TableLayout)[code-based]
查看>>
android简单demo学习系例之排版(TableLayout)[xml-based]
查看>>
bash日期格式转换(去掉无意义的零)的可选方法
查看>>
常用计算机端口解释
查看>>
转载)保护眼睛,把电脑窗口背景设置成绿颜色
查看>>
FireFox 的强大Web开发插件
查看>>
ANT的基础介绍
查看>>
MIME相关
查看>>
WAP1.0与WAP2.0页面的DTD
查看>>
如何学好C++语言
查看>>
包的设计原则
查看>>
回顾时光 详解HTML的发展史
查看>>
用移动硬盘安装win7
查看>>
MinGW与Cygwin
查看>>
C/C++/VC++的好网站
查看>>
用WEB标准进行开发
查看>>
[译]关于Android图形系统的一些事实真相
查看>>
J2ME下的Zlib/Gzip/Zip压缩相关
查看>>