-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort
More file actions
30 lines (29 loc) · 850 Bytes
/
Copy pathShellSort
File metadata and controls
30 lines (29 loc) · 850 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
/**
* Created by Bryant on 2017/6/29.
*/
public class ShellSort {
public static void main(String[] args) {
int a[]={9,1,5,8,3,7,4,6,2,10,13,12,11};
ShellSort(a);
for (int i = 0; i < a.length ; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void ShellSort(int[] a) {
int i,j;
int increment=a.length;
do {
increment=increment/3+1;
for ( i = increment; i < a.length ; i++) {
if (a[i]<a[i-increment]){
int temp=a[i];
for ( j = i-increment; j >=0 && temp<a[j] ; j-=increment) {
a[j+increment]=a[j];
}
a[j+increment]=temp;
}
}
}while (increment>1);
}
}