Data structure and algorithm (Golang implementation) (21) Sorting algorithm-insertion sort

Insert sort

Insertion sorting, generally we refer to simple insertion sorting, or direct insertion sorting. That is to say, each time a number is inserted into the already-arranged sequence to form a new sequenced sequence, and so on.

Insertion sorting belongs to the insertion sorting algorithm.

Except for me, some people are used to playing poker from the second card, compared with the first card. If the second card is smaller than the first card, it is inserted in front of the first card, so that the first two cards are both The order is sorted, and then starting from the third card, it is inserted into the first two cards in the sorted order to form three sorted cards, and the fourth card in the back is inserted into the sorted order in front. The three cards in the list are sorted.

1. Algorithm introduction

For a simple example, insert a sequence of 4 elements 4 2 9 1::

[]表示排好序

第一轮: [4] 2 9 1 拿待排序的第二个数 2,插入到排好序的数列 [4]
    与排好序的数列 [4] 比较
    第一轮进行中:2 比 4 小,插入到 4 前
第二轮: [2 4] 9 1 拿待排序的第三个数 9,插入到排好序的数列 [2 4]
    与排好序的数列 [2 4] 比较
    第二轮进行中: 9 比 4 大,不变化
第三轮: [2 4 9] 1 拿待排序的第四个数 1,插入到排好序的数列 [2 4 9]
    与排好序的数列 [2 4 9] 比较
    第三轮进行中: 1 比 9 小,插入到 9 前
    第三轮进行中: 1 比 4 小,插入到 4 前
    第三轮进行中: 1 比 2 小,插入到 2 前
结果: [1 2 4 9]

In the best case, inserting a sorted sequence of numbers, iterative N-1rounds are needed , and because the first comparison in each round, the number to be sorted is larger than the number to its left, then this round is over , do not need to compare, do not need to be exchanged, this time complexity is: O(n).

In the worst case, in each round of comparison, the number to be sorted is smaller than all the numbers in the left order, then it needs to be exchanged N-1, the first round needs to be compared and exchanged once, and the second round needs to be compared and exchanged twice. three to three, four to the fourth round, this number is: 1 + 2 + 3 + 4 + ... + N-1time complexity and bubble sort, selection sort, are: O(n^2).

Because it is from right to left, unsorted numbers are inserted into the sorted queue on the left, so insert sorting, the order of the same number will not change after sorting, this sorting algorithm is stable.

2. Algorithm implementation

package main

import "fmt"

func InsertSort(list []int) {
    n := len(list)
    // 进行 N-1 轮迭代
    for i := 1; i <= n-1; i++ {
        deal := list[i] // 待排序的数
        j := i - 1      // 待排序的数左边的第一个数的位置

        // 如果第一次比较,比左边的已排好序的第一个数小,那么进入处理
        if deal < list[j] {
            // 一直往左边找,比待排序大的数都往后挪,腾空位给待排序插入
            for ; j >= 0 && deal < list[j]; j-- {
                list[j+1] = list[j] // 某数后移,给待排序留空位
            }
            list[j+1] = deal // 结束了,待排序的数插入空位
        }
    }
}

func main() {
    list := []int{5}
    InsertSort(list)
    fmt.Println(list)

    list1 := []int{5, 9}
    InsertSort(list1)
    fmt.Println(list1)

    list2 := []int{5, 9, 1, 6, 8, 14, 6, 49, 25, 4, 6, 3}
    InsertSort(list2)
    fmt.Println(list2)
}

Output:

[5]
[5 9]
[1 3 4 5 6 6 6 8 9 14 25 49]

In nmost cases where the array size is small, we can use insert sorting, which is faster than bubble sorting and selection sorting, and even faster than any sorting algorithm.

The higher the order in the sequence, the higher the performance of the insert sort, because the higher the order of the array to be sorted, the fewer the number of insert sort comparisons.

Everyone seldom uses bubbling, direct selection, and direct insertion sorting algorithms, because these algorithms are very inefficient under unordered numbers with a large number of elements.

Series article entry

I am the star Chen, Welcome I have personally written data structures and algorithms (Golang achieve) , starting in the article to read more friendly GitBook .

Guess you like

Origin www.cnblogs.com/nima/p/12724855.html