📄 shellsort.cpp
字号:
#include <stdio.h>
#include <iostream.h>
#define MAX 255
int R[MAX];
void ShellPass(int d, int n)
{/* 希尔排序中的一趟排序,d为当前增量 */
int i,j;
for(i=d+1;i<=n;i++) /* 将R[d+1..n]分别插入各组当前的有序区 */
if(R[i]<R[i-d])
{
R[0]=R[i];j=i-d;
do {/* 查找R[i]的插入位置 */
R[j+d]=R[j];/* 后移记录 */
j=j-d; /* 查找前一记录 */
}while(j>0&&R[0]<R[j]);
R[j+d]=R[0]; /* 插入R[i]到正确的位置上 */
}
}
void Shell_Sort(int n)
{
int increment=n; /* 增量初值,不妨设n>0 */
do {
increment=increment/3+1; /* 求下一增量 */
ShellPass(increment,n); /* 一趟增量为increment的Shell插入排序 */
}while(increment>1);
}
void main()
{ int i,n;
cout << "Please input total element number of the sequence:" ;
cin >> n;
if(n<=0||n>MAX)
{
cout << "n must more than 0 and less than" << MAX << endl;
}
cout << "Please input the elements one by one:" << endl;
for(i=1;i<=n;i++)
cin >> R[i];
cout << "The sequence you input is:" ;
for(i=1;i<=n;i++)
cout << " " << R[i];
Shell_Sort(n);//希尔排序
cout << endl << endl;
cout << "The sequence after insert_sort is:" ;
for(i=1;i<=n;i++)
cout << " " << R[i];
cout << endl << endl;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -