Skip to content

Commit 1b2244b

Browse files
committed
[A] 添加希尔排序算法代码实现
1 parent 12a76a5 commit 1b2244b

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.bruis.algorithminjava.algorithm.sort;
2+
3+
/**
4+
*
5+
* 希尔排序
6+
*
7+
* @author LuoHaiYang
8+
*/
9+
public class ShellSort {
10+
public static void sort(int[] arr) {
11+
int n = arr.length;
12+
13+
int h = 1;
14+
15+
while (h < n / 3) {
16+
h = 3 * h + 1;
17+
}
18+
19+
while (h >= 1) {
20+
for (int i = h; i < n; i++) {
21+
int e = arr[i];
22+
int j = i;
23+
for (; j >= h && e < arr[j-h]; j -= h) {
24+
arr[j] = arr[j-h];
25+
}
26+
arr[j] = e;
27+
}
28+
29+
h /= 3;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)