有人提问
正在学习Python,列表里有个sort()可以排序,那么还有学习排序算法的必要吗?www.zhihu.com请看黄哥的思考:
这个问题可以这样去思考
第一个思考点
Python 的c 语言源代码sort 方法是mergesort ,每一个排序算法,都有其优缺点。
https%3A//github.com/python/cpython/blob/master/Objects/listsort.txtgithub.com
Quicksort is usually the fastest on average, but It has some pretty nasty worst-case behaviors. So if you have to guarantee no bad data gives you O(N^2), you should avoid it.
Merge-sort uses extra memory, but is particularly suitable for external sorting (i.e. huge files that don't fit into memory).
Heap-sort can sort in-place and doesn't have the worst case quadratic behavior, but on average is slower than quicksort in most cases.
Where only integers in a restricted range are involved, you can use some kind of radix sort to make it very fast.
In 99% of the cases, you'll be fine with the library sorts, which are usually based on quicksort.
请看上面三种排序算法的适用场景。当MergeSort 不能很好的解决排序时,可以选择其它Quicksort 或Heap-sort
请看10大排序算法
第二个思考点:
为了面试,当面试时,让你写一个QuickSort 、Heap-sort代码,如果写不出来,面试可能会挂!
第三个思考点:
学习计算机科学家发明的算法、有利于锻炼自己的编程能力。
所以需要学习排序等算法。
推荐看看
黄哥Python:提醒要转行当程序员的朋友,学习要分先后主次
黄哥:黄哥Python:提醒要转行当程序员的朋友,学习要分先后主次zhuanlan.zhihu.com