Skip to content

Commit e0ee4da

Browse files
authored
Create 希尔排序.cpp
1 parent cfaf601 commit e0ee4da

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

第五章/希尔排序.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include<iostream>
2+
using namespace std;
3+
/* coded by zzx 5.1 */
4+
5+
void InsertSort(int r[],int n,int start,int incr)
6+
{
7+
int i,j;
8+
for (i=start+incr;i<n;i+=incr)
9+
{
10+
11+
int temp=r[i];
12+
for (j=i;j>=incr;j-=incr)
13+
{
14+
if (temp<r[j-incr])
15+
{
16+
r[j]=r[j-incr];
17+
}
18+
else
19+
{
20+
break;
21+
}
22+
}
23+
if (i!=j)
24+
{
25+
r[j]=temp;
26+
}
27+
}
28+
}
29+
30+
void printShellSort(int r[],int g,int n)
31+
{
32+
for (;g>0;g--)
33+
{
34+
for (int i=0;i<g;i++)
35+
{
36+
InsertSort(r,n,i,g);
37+
}
38+
for (int i=0;i<n;i++)
39+
{
40+
cout<<r[i]<<' ';
41+
}
42+
cout<<endl;
43+
}
44+
}
45+
46+
int main()
47+
{
48+
int x[12];
49+
int length=0;
50+
/* 如果用下面注释掉的代码进行数组的输入会导致超时 *
51+
* 貌似是因为oj输入的时候不会有回车?*************
52+
* 不过这题也只有一个测试用例,所以你懂的 ********/
53+
// while (true)
54+
// {
55+
// cin>>x[length];
56+
// length++;
57+
// if (cin.get()=='\n')
58+
// {
59+
// break;
60+
// }
61+
// }
62+
for (;length<12;length++)
63+
{
64+
cin>>x[length];
65+
}
66+
printShellSort(x,3,length);
67+
return 0;
68+
}

0 commit comments

Comments
 (0)