> For the complete documentation index, see [llms.txt](https://jiek.gitbook.io/sorting-algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jiek.gitbook.io/sorting-algorithm/liu-hang-pai-xu-suan-fa-popular-sorting-algorithms/mao-pao-pai-xu-yu-bian-zhong-bubble-sort-and-variants/shell_sort.md).

# 希尔排序 Shellsort

## Shell sort希尔排序

@See <https://en.wikipedia.org/wiki/Shellsort> @See <https://github.com/jiek2529/java_algorithm> - ShellSort

![](/files/-M6gntdnLnbYSTERrM6c)

### principle原理

希尔排序是按照不同步长对元素进行插入排序.

## example示例

```java
public void sort(int[] list) {
        //希尔排序
        int d = list.length;
        while (true) {
            d = d / 2;
            for (int x = 0; x < d; x++) {
                for (int i = x + d; i < list.length; i = i + d) {
                    int temp = list[i];
                    int j;
                    for (j = i - d; j >= 0 && list[j] > temp; j = j - d) {
                        list[j + d] = list[j];
                    }
//                    if (i == j + d) break;
                    list[j + d] = temp;
//                    log("swap(" + i + "," + (j + d) + ")");
                }
            }
            if (d == 1) {
                break;
            }
        }
    }
```
