- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.cpp
More file actions
Latest commit
34 lines (30 loc) · 659 Bytes
/
Copy pathShellSort.cpp
File metadata and controls
34 lines (30 loc) · 659 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<iterator>
usingnamespacestd;
voidShellSort(int *H,int Hlen,int dlta[],int dlen);
voidSSort(int *H,int Hlen,int dk);
voidShellSort(int *H,int Hlen,int dlta[],int dlen)
{
for(int i=0;i<dlen;i++)
SSort(H,Hlen,dlta[i]);
}
voidSSort(int *H,int Hlen,int dk)
{
for(int i=dk;i<Hlen;i++)
if(H[i-dk]>H[i])
{
int temp = H[i];
int j=0;
for(j=i-dk;j>=0&&H[j]>temp;j -= dk)//此处j>=0
H[j+dk]=H[j];
H[j+dk]=temp;
}
}
intmain()
{
int H[] = {49,38,65,97,76,13,27,49};
int dlta[]={3,2,1};//增量数组最好为素数
ShellSort(H,8,dlta,3);
copy(H,H+8,ostream_iterator<int ,char>(cout,""));
cout<<endl;
}