
插入排序:一种简单直观的比较排序算法。它从左到右逐步构建“已排序”部分,每次取一个新元素,把它插入到已排序序列中合适的位置。适合小规模数据或数据基本有序的情况;平均与最坏时间复杂度通常为 **O(n)**,空间复杂度 **O(1)**(原地排序)。
/nsn srt/
Insertion sort is easy to implement for small arrays.
插入排序很适合用来处理小数组,且实现起来很简单。
Although insertion sort is O(n) in the worst case, it can be very fast when the input is nearly sorted because it does only a few shifts.
尽管插入排序在最坏情况下是 O(n),但当输入数据几乎有序时它可能非常快,因为只需要少量移动元素。
insertion 来自 insert(插入),源于拉丁语 inserere(放入、插入);sort 表示“排序/整理”。合在一起字面意思就是“通过插入来完成的排序方法”,强调其核心操作:把当前元素插入到前面已排序的部分中。