-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
36 lines (33 loc) · 989 Bytes
/
ShellSort.java
File metadata and controls
36 lines (33 loc) · 989 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
31
32
33
34
35
36
public class ShellSort {
int[] values;
public void sort(){
for(int gap= values.length/2; gap > 0 ; gap = (gap == 2 ? 1: (int)(gap/2.2))){
for(int i = gap ; i < values.length; i++){
int j = i;
int cmpItem = values[j];
for( ; j >= gap && cmpItem < values[j-gap]; j -= gap){
values[j] = values[j-gap];
}
values[j] = cmpItem;
}
}
}
public void print(){
for (int i=0; i < values.length; i++){
System.out.print(values[i]+" ");
}
System.out.println();
}
public void swap(int src, int dst){
int tmp = values[src];
values[src] = values[dst];
values[dst] = tmp;
}
public static void main(String[] args) {
ShellSort ss = new ShellSort();
int aa[] = {9,8,6,7,3,4,5,2,1,0};
ss.values = aa;
ss.sort();
ss.print();
}
}