归并排序
目录
具体实现
见GitHub版本库的归并排序。
算法实现
采用2层循环遍历,实现两个已排序的数组合并为一个排序数组。
- 第一层循环,从一个元素开始到最后一个元素为止。
- 选择每次循环的第一个元素为基准。
- 第二层循环,以第一层循环的第二个数开始,到未排序的数组末尾。
- 与第二个元素比较,遇到比第一个元素小的数,交换位置。
耗时分析
机器配置 ThinkPad T440s CPU i5-4300U Memory 8GB num 1000 sort cost time is 0.0038759708404541 s num 10000 sort cost time is 0.28233289718628 s num 100000 sort cost time is 36.840610027313 s
算法分析
最坏时间复杂度 О(nlog n)
最优时间复杂度 О(nlog n)
平均时间复杂度 О(nlog n)
空间复杂度 О(n)