> 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;
            }
        }
    }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET 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?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
