- 插入排序
- 增加元素数量判断加入的元素在什么位置,进行替换,外层循环次数为元素个数-1
- 时间复杂度 O(N^2)
- [34, 8, 64, 51, 32, 21]
- 排序开始
- [8, 34, 64, 51, 32, 21]
- [8, 34, 64, 51, 32, 21]
- [8, 34, 51, 64, 32, 21]
- [8, 32, 34, 51, 64, 21]
- [8, 21, 32, 34, 51, 64]
- 希尔排序(插入排序升级版)
- 通过分组之后插入排序的升级版
- 最坏时间复杂度依然为O(N^2)
- [1, 4, 2, 7, 9, 8, 3, 6]
- 开始排序
- 4 0
- 5 1
- 6 2
- 7 3
- [1, 4, 2, 6, 9, 8, 3, 7]
- 2 0
- 3 1
- 4 2
- 5 3
- 6 4
- 7 5
- [1, 4, 2, 6, 3, 7, 9, 8]
- 1 0
- 2 1
- 3 2
- 4 3
- 5 4
- 6 5
- 7 6
- [1, 2, 3, 4, 6, 7, 8, 9]
- 选择排序
- 比较第一个和其余值得大小,进行替换,第一的位置后移,外层循环次数为元素个数-1
- O(N^2)
- [1, 3, 2, 45, 65, 33, 12]
- 开始排序
- [1, 3, 2, 45, 65, 33, 12]
- [1, 2, 3, 45, 65, 33, 12]
- [1, 2, 3, 45, 65, 33, 12]
- [1, 2, 3, 12, 65, 33, 45]
- [1, 2, 3, 12, 33, 65, 45]
- [1, 2, 3, 12, 33, 45, 65]
- 冒泡排序
- 依次比较相邻的两个数,将小数放在前面,大数放在后面
- O(N^2)
- [6, 3, 8, 2, 9, 1]
- 开始排序
- [3, 6, 2, 8, 1, 9]
- [3, 2, 6, 1, 8, 9]
- [2, 3, 1, 6, 8, 9]
- [2, 1, 3, 6, 8, 9]
- [1, 2, 3, 6, 8, 9]
- 计数排序
- 创建数组,通过索引判断值,通过值判断数量
- O(N)
- [2, 5, 3, 0, 2, 3, 0, 3]
- 开始排序
- [2, 0, 2, 3, 0, 1]
- [0, 0, 2, 2, 3, 3, 3, 5]
- 基数排序(桶排序)
- 先按照个位数排序之后按照十位排序
- O(N)
- [73, 22, 93, 43, 55, 14, 28, 65, 39, 81]
- 开始排序
- [14, 22, 28, 39, 43, 55, 65, 73, 81, 93]
- 归并排序
- 利用完全二叉树,递归分解表至一个或两个进行比较排序,之后合并排序
- O(nlogn)
- [51, 46, 20, 18, 65, 97, 82, 30, 77, 50]
- 开始排序
- [46, 51, 20, 18, 65, 97, 82, 30, 77, 50]
- [20, 46, 51, 18, 65, 97, 82, 30, 77, 50]
- [20, 46, 51, 18, 65, 97, 82, 30, 77, 50]
- [18, 20, 46, 51, 65, 97, 82, 30, 77, 50]
- [18, 20, 46, 51, 65, 82, 97, 30, 77, 50]
- [18, 20, 46, 51, 65, 30, 82, 97, 77, 50]
- [18, 20, 46, 51, 65, 30, 82, 97, 50, 77]
- [18, 20, 46, 51, 65, 30, 50, 77, 82, 97]
- [18, 20, 30, 46, 50, 51, 65, 77, 82, 97]