具体实现
见GitHub版本库的选择排序。
算法实现
采用2层循环遍历实现。
1. 第一层循环,从一个元素开始到最后一个元素为止。
2. 选择每次循环的第一个元素为基准。
3. 第二层循环,以第一层循环的第二个数开始,到未排序的数组末尾。
4. 与第二个元素比较,遇到比第一个元素小的数,交换位置。
耗时分析
机器配置 ThinkPad T440s CPU i5-4300U Memory 8GB
num 1000 sort cost time is 0.012492895126343 s
num 10000 sort cost time is 1.2727150917053 s
num 100000 sort cost time is 146.14201211929 s
算法分析
最坏时间复杂度 О(n²)
最优时间复杂度 О(n²)
平均时间复杂度 О(n²)
空间复杂度 О(n) total, O(1) auxiliary
参考链接
打赏作者