-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShellSort.java
41 lines (35 loc) · 1.19 KB
/
ShellSort.java
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
35
36
37
38
39
40
41
package sorting;
public class ShellSort{
public static void main(String []args){
int[] a = {9,8,7,6,5,4,3,2,1};
System.out.println("Before Shell Sort:");
for(int i=0; i<a.length; i++)
System.out.print(a[i] + " ");
shellSort(a);
System.out.println("\n\nAfter Shell Sort:");
for(int i=0; i<a.length; i++)
System.out.print(a[i] + " ");
}
//Performs a shell sort
public static int[] shellSort(int[] array){
int h = 1;
while (h < array.length) h = 3*h + 1;
while (h > 0){
h = h/3;
for (int k = 0; k < h; k++){
for (int i = h+k; i < array.length; i+=h){
int key = array[i];
int j = i-h;
while (j>=0 && array[j] > key){
array[j+h] = array[j];
j-=h;
}
array[j+h] = key;
//-> invariant: array[0,h,2*h..j] is sorted
}
}
//->invariant: each h-sub-array is sorted
}
return array;
};
}