MCPcopy Index your code
hub / github.com/hustcc/JS-Sorting-Algorithm / ShellSort

Class ShellSort

src/java/main/ShellSort.java:6–33  ·  view source on GitHub ↗

希尔排序

Source from the content-addressed store, hash-verified

4 * 希尔排序
5 */
6public class ShellSort implements IArraySort {
7
8 @Override
9 public int[] sort(int[] sourceArray) throws Exception {
10 // 对 arr 进行拷贝,不改变参数内容
11 int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
12
13 int gap = 1;
14 while (gap < arr.length) {
15 gap = gap * 3 + 1;
16 }
17
18 while (gap > 0) {
19 for (int i = gap; i < arr.length; i++) {
20 int tmp = arr[i];
21 int j = i - gap;
22 while (j >= 0 && arr[j] > tmp) {
23 arr[j + gap] = arr[j];
24 j -= gap;
25 }
26 arr[j + gap] = tmp;
27 }
28 gap = (int) Math.floor(gap / 3);
29 }
30
31 return arr;
32 }
33}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected