Python sorted 方法 使用例子和稳定性分析

转自http://longriver.me/?p=325

经常使用python built-in sort 方法,使用方法例子如下:

Sorted 方法用起来很方便,当alist的元素是个对象的时候可以自己定义对对象的排序,如

有的时候我们需要连续使用sorted对list的elements做多次排序,这样就需要考虑python 内置的sorted 的稳定性问题:

    In early python-versions, the sort function implemented a modified version of quicksort. However, it was deemed unstable and as of 2.3 they switched to using an adaptive mergesort algorithm.

可见早期的sort实现的是quicksort,是不稳定,到了python2.3之后,改成了适应性的归并排序算法,是稳定算法了。

如果想看sort的源代码,可以看这里

posted on 2014-03-14 14:02  Harveyaot  阅读(763)  评论(0编辑  收藏  举报

导航