-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellSort.cpp
More file actions
27 lines (24 loc) · 476 Bytes
/
Copy pathshellSort.cpp
File metadata and controls
27 lines (24 loc) · 476 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
//希尔排序
#include<iostream>
using namespace std;
void shellSort(int a[], int len){
if(a==NULL||len<=0)return ;
int i,j,h, temp;
for(h=len/2;h>0;h=h/2){
//下面与直接插入排序一样 升序排列
for(i=h;i<len;++i){
temp=a[i];
for(j=i-h;j>=0&&temp<a[j];j=j-h){
a[j+h]=a[j];
}
a[j+h]=temp;
}
}
}
int main(){
int a[]={7,3,5,8,9,1,2,4,6};
cout<<"sort Array is:\n";
shellSort(a,9);
for(int i=0;i<9;++i)cout<<a[i]<<" ";
return 0;
}