-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathShellSort.java
67 lines (53 loc) · 1.66 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
import java.util.Random;
class Array{
int[] array;
int nElems;
public Array(int maxElems){
array = new int[maxElems];
nElems = 0;
}
public void insert(int elem){
array[nElems++] = elem;
}
public void display(){
for(int i=0; i<nElems; i++){
System.out.print(array[i] + " ");
}
System.out.println();
}
public void shellsort(){
int h = 1;
// Get the largest number from Knuth Sequence that is less than number of Elements in array.
while(h <= nElems/3){
h = 3*h + 1;
}
// Perform sorting
while(h > 0){ // h will decrease with each iteration.
for(int outer = h; outer<nElems; outer++){
// A temp variable for the array under consideration.
int temp = array[outer];
int index = outer; // Keep track of the index that we will be comparing.
while(index>=h && array[index-h] > temp){
array[index] = array[index-h]; // Put the smaller element in the bigger's place.
index -= h;
}
array[index] = temp;
}
h = (h-1) / 3;
}
}
}
class ShellSort{
public static void main(String[] args) {
Array array = new Array(20);
Random random = new Random();
for(int i = 0; i<10; i++){
array.insert(random.nextInt(100));
}
System.out.println("Array before sorting:");
array.display();
array.shellsort();
System.out.println("Array after sorting:");
array.display();
}
}