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-1
rounds 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-1
time 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 n
most 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 .
- Data structure and algorithm (Golang implementation) (1) A simple introduction to Golang-Preface
- Data structures and algorithms (Golang implementation) (2) A simple introduction to Golang-packages, variables and functions
- Data structure and algorithm (Golang implementation) (3) A simple introduction to Golang-flow control statement
- Data structures and algorithms (Golang implementation) (4) A simple introduction to Golang-structures and methods
- Data structure and algorithm (Golang implementation) (5) A simple introduction to Golang-interface
- Data structure and algorithm (Golang implementation) (6) A simple introduction to Golang-concurrency, coroutines and channels
- Data structure and algorithm (Golang implementation) (7) A simple introduction to Golang-standard library
- Data Structure and Algorithm (Golang Implementation) (8.1) Basic Knowledge-Preface
- Data Structure and Algorithm (Golang Implementation) (8.2) Basic Knowledge-Divide and Conquer and Recursion
- Data structure and algorithm (Golang implementation) (9) Basic knowledge-algorithm complexity and progressive symbol
- Data structure and algorithm (Golang implementation) (10) Basic knowledge-the main method of algorithm complexity
- Data Structures and Algorithms (Golang Implementation) (11) Common Data Structures-Preface
- Data structures and algorithms (Golang implementation) (12) Common data structures-linked lists
- Data structures and algorithms (Golang implementation) (13) Common data structures-variable-length arrays
- Data structures and algorithms (Golang implementation) (14) Common data structures-stack and queue
- Data Structures and Algorithms (Golang Implementation) (15) Common Data Structures-List
- Data Structures and Algorithms (Golang Implementation) (16) Common Data Structures-Dictionary
- Data structures and algorithms (Golang implementation) (17) Common data structures-trees
- Data Structure and Algorithm (Golang Implementation) (18) Sorting Algorithm-Preface
- Data structure and algorithm (Golang implementation) (19) Sorting algorithm-bubble sorting
- Data structure and algorithm (Golang implementation) (20) Sorting algorithm-selection sorting
- Data structure and algorithm (Golang implementation) (21) Sorting algorithm-insertion sort
- Data structure and algorithm (Golang implementation) (22) Sorting algorithm-Hill sorting
- Data structure and algorithm (Golang implementation) (23) Sorting algorithm-merge sorting
- Data structure and algorithm (Golang implementation) (24) Sorting algorithm-priority queue and heap sorting
- Data structure and algorithm (Golang implementation) (25) Sorting algorithm-quick sorting
- Data Structure and Algorithm (Golang Implementation) (26) Lookup Algorithm-Hash Table
- Data structure and algorithm (Golang implementation) (27) Search algorithm-binary search tree
- Data structure and algorithm (Golang implementation) (28) Search algorithm-AVL tree
- Data structure and algorithm (Golang implementation) (29) Finding algorithm-2-3 tree and left-leaning red-black tree
- Data structure and algorithm (implemented by Golang) (30) Finding algorithm-2-3-4 tree and ordinary red-black tree