希尔排序
| 4 | * 希尔排序 |
| 5 | */ |
| 6 | public 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected