-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
52 lines (44 loc) · 1.34 KB
/
Copy pathShellSort.java
File metadata and controls
52 lines (44 loc) · 1.34 KB
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
public class ShellSort {
static void shellsort(int[] unsorted, int len)
{
int group, i, j, temp;
for (group = len / 2; group > 0; group /= 2)
{
/* for (i = group; i < len; i++)
{
for (j = i - group; j >= 0; j -= group)
{
if (unsorted[j] > unsorted[j + group])
{
temp = unsorted[j];
unsorted[j] = unsorted[j + group];
unsorted[j + group] = temp;
}
}
}*/
for(i = group; i < len; i++){
temp = unsorted[i];
for(j = i - group; j >= 0 && unsorted[j] > temp; j -= group);
j += group;
for(int k = i; k > j; k -= group){
unsorted[k] = unsorted[k - group];
}
unsorted[j] = temp;
}
}
}
public static void main(String[] args) {
int[] num = {9,8,7,6,5,4,3,2,1,0};
System.out.println("排序前的数据:");
for(int i = 0; i < num.length; i++){
System.out.print(num[i] + " ");
}
System.out.println();
ShellSort.shellsort(num, num.length);
System.out.println("排序后的数据:");
for(int i = 0; i < num.length; i++){
System.out.print(num[i] + " ");
}
System.out.println();
}
}