-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathshellSort.java
72 lines (58 loc) · 1.07 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Solved By: Sandeep Ranjan (1641012352)
*/
class ArrayShell {
private int[] a;
private int nElems;
public ArrayShell(int max) {
a = new int[max];
nElems = 0;
}
public void insert(int value) {
a[nElems] = value;
nElems++;
}
public void display() {
for(int j=0; j<nElems; j++)
System.out.print(a[j] + " ");
System.out.println("");
}
public void shellSort() {
// Knuth’s Interval Sequence
int h = 1;
while(h <= nElems/3)
h = h*3 + 1;
while(h > 0) {
for(int out=h; out<nElems; out++) {
int temp = a[out];
int in = out;
while(in > h-1 && a[in - h] >= temp) {
a[in] = a[in-h];
in -= h;
}
a[in] = temp;
}
// Reducing Knuth’s
h = (h-1) / 3;
}
return;
}
}
class ShellSort {
public static void main(String[] args) {
int maxSize = 11;
ArrayShell arr = new ArrayShell(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.shellSort();
arr.display();
}
}