> 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/jian-dan-pai-xu-simple-sorts/insertion_sort.md).

# 插入排序 Insertion sort

## Insertion sort插入排序算法

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

![](https://2531019738-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6gnm_fO-nBQv_cernA%2Fsync%2F96c1b12d5cc82d607fbf191908a8444ec517ae9e.gif?generation=1588815962695668\&alt=media)

### principle原理

从列表第一位开始向高位进行排序 每次取一位向下沉底排序 双for循环推进排序

## example示例

```java
@Override
    public void sort(int[] list) {
        if (list != null && list.length > 1) {
            for (int i = 1; i < list.length; i++) {
                for (int j = i; j > 0; j--) {
                    if (list[j] < list[j - 1]) {
                        swap(list, j, j - 1);
                    } else {
                        break;
                    }
                }
            }
        }
    }
```
